【问题标题】:read Data URL with File API polyfill使用 File API polyfill 读取数据 URL
【发布时间】:2013-05-18 16:57:15
【问题描述】:

我正在尝试使用文件 API 库 (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) 返回一个报告错误的对象:“filereader_not_support_DataURL”。

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

谢谢。

-- 山姆。

【问题讨论】:

    标签: javascript html fileapi polyfills


    【解决方案1】:

    功能是通过 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

    【讨论】:

      【解决方案2】:

      我认为错误信息很清楚。您想使用此 polyfill 将文件作为数据 URL 读取,它会告诉您它不支持数据 URL。所以我认为没有其他方法可以做到这一点。

      【讨论】:

      • 这也是我最后的假设。有演示代码显示数据 URL 使用情况;但也许它还没有在库中实现。谢谢。
      猜你喜欢
      • 2019-02-02
      • 1970-01-01
      • 1970-01-01
      • 2016-10-26
      • 1970-01-01
      • 2010-11-04
      • 1970-01-01
      • 2015-03-19
      • 2015-05-08
      相关资源
      最近更新 更多