doushen2154 2018-08-03 10:11
浏览 86

在Javascript #EXIF数据中将对象转换为JSON的JSON.stringify问题

Object to json is easy but don't know what happening with my code.

So i wanted to read image metadata using PHP ( most important is image created date-time ) but iPhone and other phone do not send this info with image, its working if try to upload using PC. but if image is compressed then we lost all metadata.

So it's not useful to read metadata using php then i decided to read via JavaScript and for that i am using exif.js.

for multiple upload everything is working fine but when i try to convert object to json, i am getting empty string.

Here is my code

$('#ServicePhotos').change(function(){ 
    $('#err_msg').html('');    
    var objects = {};
    var lg = this.files.length;
    for(var i = 0; i < lg; i++){
        (function(file){
            var file = file;
            var file_path = file.name;
            if(file_path){
                var startIndex = (file_path.indexOf('\\') >= 0 ? file_path.lastIndexOf('\\') : file_path.lastIndexOf('/'));
                var filename = file_path.substring(startIndex);
                if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
                filename = filename.substring(1); }
                //console.log('uploading image ' + filename);
            }
            EXIF.getData(file, function() {
                var exifData = EXIF.pretty(this);
                if (exifData) {
                    var allMetaData = EXIF.getAllTags(this);
                    var DateTime = EXIF.getTag(this, "DateTime");
                    objects[filename] = {'created':DateTime};
                }
            });
        })(this.files[i]);
    }
    console.log(objects); /*showing all data */
    var myJSON = JSON.stringify(objects);
    console.log(myJSON); /* EMPTY ??? */
    $('#err_msg').html(myJSON);
    $('#meta_data').val(myJSON);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/exif-js/2.3.0/exif.js"></script>

<input id="ServicePhotos" type="file"  />
<div id="err_msg"></div>

Now here "var myJSON" is NOT showing json data

Here is console.log

enter image description here

</div>
  • 写回答

1条回答 默认 最新

  • dongpu9481 2018-08-03 10:23
    关注

    If you can't stringify it probably means your object doesn't have enumerable properties. You can test if your object properties are enumerable, if true, you should be able to use JSON.stringify()

    From MDN Web Docs (https://developer.mozilla.org):

    "All the other Object instances (including Map, Set, WeakMap, and WeakSet) will have only their enumerable properties serialized."

    From documentation:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大