简体   繁体   English

HTML5 FileReader:无法转换JavaScript参数

[英]HTML5 FileReader: Could not convert JavaScript argument

I got a strange error while trying to read a local file with FileReader (I am using Firefox 16.0.2): 尝试使用FileReader读取本地文件时出现一个奇怪的错误(我使用的是Firefox 16.0.2):

[20:57:48.440] reader = new FileReader()
[20:57:48.453] [object FileReader]
--
[20:58:15.104] reader.onload= function(evt) {alert('ok');}
[20:58:15.114] (function (evt) {alert("ok");})
--
[20:58:32.162] reader.readAsText('test.txt');
[20:58:32.170] [Exception... "Could not convert JavaScript argument arg 0 [nsIDOMFileReader.readAsText]"  nsresult: "0x80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)"  location: "JS frame :: Web Console :: <TOP_LEVEL> :: line 1"  data: no]

Any ideas of how to fix it? 关于如何解决的任何想法?

A FileReader takes a File or Blob parameter. FileReader带有File或Blob参数。 Not some kind of string that resembles a file name. 不是某种类似于文件名的字符串。 The File comes from a drag/drop action of an input type=file. File来自输入type = file的拖放操作。

See this html5rocks article for more details or the code below for reading images. 有关更多详细信息,请参见此html5rocks文章;有关读取图像的信息,请参见下面的代码。

$(function () {
        if (Modernizr.draganddrop) {
            var dropTarget = $('#dropTarget').get(0);

            dropTarget.addEventListener('dragover', function (e) {
                e.preventDefault();
            });

            dropTarget.addEventListener('drop', function (e) {
                e.preventDefault();

                if (e.dataTransfer.files === undefined) {
                    // IE doesn't support file drops yet.
                    $('#dropTarget').text('Drag & Drop of files is not supported');
                    return;
                }

                $.each(e.dataTransfer.files, function () {
                    var file = this;
                    var reader = new FileReader();
                    reader.onload = function () {
                        $('<img>')
                            .attr('title', file.name)
                            .attr('src', this.result)
                            .css('width', '75%')
                            .appendTo('#images');
                    };
                    reader.readAsDataURL(file);
                });
            });
        }
        else {
            $('#dropTarget').text('Drag & Drop is not supported');
        }
    });

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

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