使用File API polyfill读取数据URL [英] read Data URL with File API polyfill

查看:246
本文介绍了使用File API polyfill读取数据URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use the File API library (https://github.com/mailru/FileAPI) as a fallback for browsers which do not support the File API, in order to read a file as a Data URL and pass that on to another function.

我有这段代码:

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。

However, console.log(evt) returns an Object which reports error: "filreader_not_support_DataURL".

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

Have I done something wrong with File API polyfill or is there a shim or other polyfill I have to use to get data URLs to work?

谢谢。

- Sam。

推荐答案

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

Feature is implemented yet via flash - thanks to the author of FileAPI plugin.

// 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

这篇关于使用File API polyfill读取数据URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆