HTML5 FileReader的问题 [英] Problems with HTML5 FileReader

查看:86
本文介绍了HTML5 FileReader的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我的项目中运行FileReader函数,但它仅适用于pas.Mon项目,因此我在目录中列出了所有文件.一切正常.但是,当我在列表中选择一个文件时(我的文件是本地文件),最终会出现此错误,我不明白:

I try to run the FileReader function in my project but it only works pas.Mon project works like so I list all files in a directory. It all works. But when I select a file in the list (my files are local), I end up with this error I do not understand:

未捕获的TypeError:无法在'FileReader'上执行'readAsText':参数不是Blob.

这是我的代码:

GetFileName function (fs) {
     var target = event.target || event.srcElement;

     var childs = target.parentNode.childNodes;
     {for (i = 0; i ++; i <childs.length)
         if (target == childs [i]) break;
     }

     console.log (i); // Index li
     console.log (event.target.innerHTML); // li value

     filename = "~ / Downloads / snippets" + event.target.innerHTML;

var fr = new FileReader ();
fr.onload = function (e) {
     console.log (e.target.result);
};
fr.readAsText (filename);
}

有人知道什么不起作用吗?

Does anyone have an idea of what does not work?

预先感谢您的帮助!

推荐答案

我认为问题在于您的filename变量设置为:

I think the problem is that your filename variable is set to:

filename = "~ / Downloads / snippets" + event.target.innerHTML;

...这是一个字符串,而不是FileReader上的readAsText函数期望的对象.

...which is a String, rather than the object that's expected by the readAsText function on FileReader.

尝试这样的方法:

var fileInputElement = document.getElementById("fileInputElement");
fr.readAsText(fileInputElement.files[0]);

其中"fileInputElement"是类型为file的HTML <input>元素的ID.

Where "fileInputElement" is the ID of the HTML <input> element of type file.

这可能与您正在做的事情不完全吻合,但是要在不看到更多JavaScript和HTML的情况下很难分辨出来.

This may not fit exactly with what you're doing, but it's not easy to tell without seeing more of your JavaScript and HTML.

这篇关于HTML5 FileReader的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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