javascript 文件 API FileReader() readAsText 函数不适用于类型“文件”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14362496/
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
File API FileReader() readAsText function not working for type 'file'
提问by CraigDub
I created a test for local file handling, but can't get the 'file' type to return any contents in Chrome or Firefox. Chrome gives no error and acts like there was never a call to the FileReader(), and Firefox gives a strange error. FileReader() works fine on the Blob type. Any ideas? See test code below:
我为本地文件处理创建了一个测试,但无法获取“文件”类型以在 Chrome 或 Firefox 中返回任何内容。Chrome 没有给出任何错误,就像从来没有调用 FileReader() 一样,而 Firefox 给出了一个奇怪的错误。FileReader() 在 Blob 类型上工作正常。有任何想法吗?见下面的测试代码:
<head>
<script language="JavaScript" type="text/javascript">
var ContentString='(empty)';
var reader = new FileReader();
var testBLOB = new Blob(['Test Blob'], { "type" : "text/plain" });
reader.onload = function(event) {ContentString = event.target.result;};
reader.onerror = function(event) {alert('Load error!');};
function WriteContents(Iteration,FileOrBlob)
{
if (Iteration==0)
{
if (FileOrBlob==0)
{
try
{
var fileUpload=document.getElementById("inputFile");
//alert(fileUpload.type);
reader.readAsText(fileUpload/*, "UTF-8"*/);
}
catch(err) {alert(err.message);}
}
else
{
reader.readAsText(testBLOB/*, "UTF-8"*/);
}
}
document.getElementById("Iteration").innerHTML=Iteration;
document.getElementById("ContentString").innerHTML=ContentString;
document.getElementById("error").innerHTML=reader.error;
document.getElementById("readyState").innerHTML=reader.readyState;
document.getElementById("result").innerHTML=reader.result;
setTimeout('WriteContents('+(Iteration+1)+','+FileOrBlob+');',5000);
}
if (window.File && window.FileReader && window.FileList && window.Blob) {document.write('OK<br>');} else {document.write('Browser Error<br>');}
</script>
</head>
<br>
<input type="file" id="inputFile" name="inputFile" onchange="WriteContents(0,0);">
<input type="button" onclick="WriteContents(0,1);" value="Use Test Blob">
<br><br>
<table border=1>
<tr><td>Iteration</td><td id="Iteration">Choose File or Blob Above (one time only please!)</td></tr>
<tr><td>ContentString</td><td id="ContentString"></td></tr>
<tr><td>error</td><td id="error"></td></tr>
<tr><td>readyState</td><td id="readyState"></td></tr>
<tr><td>result</td><td id="result"></td></tr>
采纳答案by CraigDub
Found the answer in this post:
在这篇文章中找到了答案:
File API: Returns file reference (object) or whole file content (string)?
文件 API:返回文件引用(对象)还是整个文件内容(字符串)?
You need to access the blob part of the 'file' - these lines
您需要访问“文件”的 blob 部分 - 这些行
var fileUpload=document.getElementById("inputFile");
reader.readAsText(fileUpload/*, "UTF-8"*/);
should be
应该
var fileUpload=document.getElementById("inputFile").files[0];
reader.readAsText(fileUpload/*, "UTF-8"*/);
Only 9 characters out!!
只出9个字!!