Javascript previews with new FileReader API and DataURLs seem inefficient

筅森魡賤 提交于 2019-12-30 10:01:57

问题


I am using the new FileReader API to preview images before upload. This is done using DataURLs. But DataURLs can be massive if the images are large. This is especially a problem for me as the user may upload multiple images at a time and previewing the bunch has actually slowed my browser considerably and actually crashed chrome a few times.

Is there any alternative to using DataURLs for previewing images on the client before upload?


回答1:


You can also store data on the client's disk (in another location so that you can access the file using JavaScript). This article is quite extensive when it comes to this subject:

http://www.html5rocks.com/en/tutorials/file/filesystem/

It's not supported on all browsers though.

You have to request storage space (the file system), then create a file, write data to it, and finally fetch the URL:

window.requestFileSystem(window.PERSISTENT, 5*1024*1024, function(fs) {
    fs.root.getFile(filename, {create: true}, function(fileEntry) {
        fileEntry.createWriter(function(fileWriter) {
            var arr = new Uint8Array(data.length);

            // fill arr with image byte data here

            var builder = new BlobBuilder();
            builder.append(arr.buffer);
            var blob = builder.getBlob();

            fileWriter.write(blob);

            location.href = fileEntry.toURL(); // navigate to file. The URL does not contain the data but only the path and filename.
        });
    });
}, function() {});


来源:https://stackoverflow.com/questions/6723931/javascript-previews-with-new-filereader-api-and-dataurls-seem-inefficient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!