使用 File API polyfill 读取数据 URL

2024-05-16

我正在尝试使用文件 API 库(https://github.com/mailru/FileAPI https://github.com/mailru/FileAPI)作为不支持文件 API 的浏览器的后备,以便将文件作为数据 URL 读取并将其传递给另一个函数。

我有这个代码:

function handleFileSelect(event) {
    // If supported, use native File API.
    if(window.FileReader != null) {
        console.log('Using native File API.'); // !
        var files = event.target.files;
        for(var i = 0, f; f = files[i]; i++) {
            var reader = new FileReader();
            reader.onload = function(event) {
                insertImage(event.target.result);
            };
            reader.readAsDataURL(f);
         }
    }
// Otherwise, use fallback polyfill for browsers not supporting native API.
else {
    console.log('Using polyfill for File API.'); // !
    var file = FileAPI.getFiles(event.target);
    FileAPI.readAsDataURL(file, function(evt) {
        console.log(evt);
    });
  }
}

但是,console.log(evt) 返回一个报告错误的对象:“filreader_not_support_DataURL”。

我是否在使用 File API polyfill 时做错了什么,或者是否需要使用垫片或其他 polyfill 才能使数据 URL 正常工作?

Thanks.

-- Sam.


该功能已通过 Flash 实现 - 感谢 FileAPI 插件的作者。

// bind to your upload container
var el = document.getElementById('uploadElementId');
FileAPI.event.on(el, 'change', function (evt/**Event*/) {
    var files = FileAPI.getFiles(evt);
    // get Data URL for the first file
    FileAPI.readAsDataURL(files[0], function (dataUrlObject) {
        if (dataUrlObject.type == 'load') {
            // Success                    
            var dataUrl = dataUrlObject.result;
        }
    });
});

整个线程在这里:https://github.com/mailru/FileAPI/issues/153 https://github.com/mailru/FileAPI/issues/153

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 File API polyfill 读取数据 URL 的相关文章