javascript ReadAsText() 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18021367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ReadAsText() arguments
提问by First Last
I'm totally new to javascript (Hello World Level), I've made a lot of searches in google but couldn't even find one result explaining what arguments the ReadAsText() method get. I've tried to put the address of a file and encoding format but I keep receiving "Type mismatch" error. I'm using it this way :
我是 javascript(Hello World Level)的新手,我在 google 中进行了大量搜索,但甚至找不到解释 ReadAsText() 方法获得哪些参数的结果。我试图输入文件的地址和编码格式,但我一直收到“类型不匹配”错误。我是这样使用的:
reader.readAsText ("d:\file.txt", "UTF-8");
I know that this question is not really a match for StackOverFlow but if I found any result in google I wouldn't post it here.
我知道这个问题与 StackOverFlow 不太匹配,但如果我在 google 中找到任何结果,我就不会在这里发布。
By the way when I use it this way it works very well:
顺便说一句,当我以这种方式使用它时,效果很好:
function FileReader (f) {
var reader = new FileReader();
reader.readAsText (f);
var text = reader.result();
}
and then
接着
<input type="file" onchange="readfile(this.files[0])"></input>
but I don't know why it shows error when I type in the address of the file statically.
但是我不知道为什么当我静态输入文件地址时它会显示错误。
回答by Musa
readAsText
takes a Blob
or File
object as the first argument; any argument of a different type will cause the type error. In your working example you pass a File object which is why it works.
readAsText
将一个Blob
或File
对象作为第一个参数;任何不同类型的参数都会导致类型错误。在您的工作示例中,您传递了一个 File 对象,这就是它起作用的原因。