简体   繁体   English

JavaScript对象到文件对象

[英]JavaScript object to file object

I'm working on adding images to page, do something with collection of added images (preview etc) and finally I want them save. 我正在将图像添加到页面上,对添加的图像进行收集(预览等),最后我希望将它们保存。 Everything is cool until the files object is used to show or save the photo. 直到使用files对象显示或保存照片之前,一切都很棒。

var input = document.getElementById('files');
var files = input.files;

as it is an array of objects read only - it is impossible to manipulate it freely. 因为它是只读的对象数组,所以无法自由地对其进行操作。 For working with that array friendly I maped it like that: 为了使用友好的数组,我将其映射为:

var addedFiles = added(files); 
function added(from) {
    return $.map(from, function (i) {
         var x = { lastModified: i.lastModified, lastModifiedDate: i.lastModifiedDate, name: i.name, size: i.size, type: i.type, webkitRelativePath: i.webkitRelativePath }
         return x;
    });
}

... then do something with those files - and I want to preview, and then save - but for example during preview I get an error: ...然后对那些文件做一些操作-我想预览,然后保存-但例如在预览过程中出现错误:

Uncaught TypeError: Failed to execute 'readAsDataURL' on 'FileReader': parameter 1 is not of type 'Blob'.

    function readImage(file) {
        var reader = new FileReader();
        reader.addEventListener("load", function () {
            var image = new Image();

            image.addEventListener("load", function () {
                preview.innerHTML += drawHtml(this, file);
                window.URL.revokeObjectURL(image.src); //blob version
            }); 
            image.src = reader.result; //file version
            image.src = window.URL.createObjectURL(file) //blob version
        });
        reader.readAsDataURL(file); // here fire the error
    }

When I pass for testing originally file obj to above code every thing is working. 当我通过测试原始file obj到上面的代码时,一切都正常。

Question: How to create custom obj (in my case array of obj) that can be parse to file obj 问题:如何创建可解析为file obj的自定义obj(在我的情况下为obj数组)

PS In project I'm using jquery and javascript PS在项目中,我正在使用jQuery和JavaScript

Rather than mapping the File objects to new, incompatible objects, you could instead wrap them with the additional things you need, but then use the underlying original files when reading them: 与其将File对象映射到新的不兼容的对象,不如它们与所需的其他东西包装在一起,但是在读取它们时使用基础原始文件:

const fileSelections = Array.prototype.map.call(input.files, file => ({
  // This will let you get to the underlying file in the wrapper objects
  file,

  // If you want pass-throughs, you can do stuff like this:
  get lastModified() { return file.lastModified },

  // And you can add your own properties/methods as you please
});

function readImage(fileSelection) {
  // Unwrap the file
  const file = fileSelection.file;

  const reader = new FileReader();
  reader.addEventListener("load", function () {
    const image = new Image();

    image.addEventListener("load", function () {
      preview.innerHTML += drawHtml(this, file);
      window.URL.revokeObjectURL(image.src); //blob version
    }); 
    image.src = reader.result; //file version
    image.src = window.URL.createObjectURL(file) //blob version
  });
  reader.readAsDataURL(file);
}

correct answer is blob - it's something amazing for me. 正确答案是一滴-对我来说真是太神奇了。

//from is the array of obj - files
  function added(from) {
                    var out = [];
                    for (var i = 0; i < from.length; i++) {
                        (function (obj) {
                            var readerBase64 = new FileReader();
                            var obj = from[i];
                            readerBase64.addEventListener("load", function () {
                                var fileBase64 = readerBase64.result;
                                var row = { name: obj.name, size: obj.size, type: obj.type, base64: fileBase64 }
                                out.push(row);
                            });
                            readerBase64.readAsDataURL(obj); 
                        })(from[i]);
                    }
                    return out;
                }

'out' is a table of my own objects with base64, so I can create images for preview and 'do something functions' in the end I'm going to use base64 for create files. “ out”是我自己的带有base64的对象的表,因此我可以创建图像进行预览,最后使用“ base64”创建文件。

here link for question related to my next step - creating img from blob (where I'm using additional lib b64toBlob) 与我的下一步相关的问题的此处链接-从blob创建img(我在其中使用其他lib b64toBlob)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM