[英]FileReader read file undefined result
I wish to read the contents of an upload file into a Javascript variable.我希望将上传文件的内容读入 Javascript 变量。
The program used to work using file.getAsBinary but this is now deprecated and needs to be updated to use FileReader()该程序曾经使用 file.getAsBinary 工作,但现在已弃用,需要更新以使用 FileReader()
A form has a file upload selection an onsubmit runs a function uploadDB() in a javascript.表单有一个文件上传选择,onsubmit 在 javascript 中运行一个函数 uploadDB()。
The variable dbname is passed okay as is the file name from the upload selection I can see it with an alert or console.log.变量 dbname 和上传选择中的文件名一样可以正常传递,我可以通过警报或 console.log 看到它。
The final bfile variable to receive the file text is undefined.接收文件文本的最终 bfile 变量未定义。 I have tried both readAsText and ReadAsBinary but bfile is undefined.
我已经尝试了 readAsText 和 ReadAsBinary 但 bfile 未定义。 The var declaration is in function uploadDB() and should be global to the inner function.
var 声明在函数 uploadDB() 中并且应该是内部函数的全局变量。 If scope is somehow a problem how do I get the result in the bfile variable.
如果范围在某种程度上是一个问题,我如何在 bfile 变量中获得结果。
Its probably simple but I cannot get it to work.它可能很简单,但我无法让它工作。 Can someone suggest something please.
有人可以建议一些东西吗。 The html section is;
html 部分是;
<form onsubmit="uploadDB()"; method="post">
Database <input type=text name="dbName" id="dbName" size=20>
<input type=file id="metaDescFile" size=50 >
<input type=submit value="Submit">
</form>
The js script is similar to (extraneous stuff edited out); js 脚本类似于(编辑掉无关的东西);
<script language="javascript" type="text/javascript">
<!--
var uploadDB = function() {
var input = document.getElementById('metaDescFile');
var fname = document.getElementById('metaDescFile').value;
var file=input.files[0];
var bfile;
reader = new FileReader();
reader.onload = function(e) { bfile = e.target.result }
reader.readAsText(file);
alert(bfile); // this shows bfile as undefined
// other stuff
}
as bfile gets set in the onload
callback you won't be able to access outside that callback because the code is evaluated before the callback is fired.由于 bfile 在
onload
回调中设置,您将无法在该回调之外访问,因为在触发回调之前评估代码。
Try this:试试这个:
reader = new FileReader();
reader.onload = function(e) {
bfile = e.target.result
alert(bfile); // this shows bfile
}
reader.readAsText(file);
Here is one answer to get the actual final byte array , just using FileReader
and ArrayBuffer
:这是获取实际最终字节数组的一个答案,只需使用
FileReader
和ArrayBuffer
:
const test_function = async () => {
... ... ...
const get_file_array = (file) => {
return new Promise((acc, err) => {
const reader = new FileReader();
reader.onload = (event) => { acc(event.target.result) };
reader.onerror = (err) => { err(err) };
reader.readAsArrayBuffer(file);
});
}
const temp = await get_file_array(files[0])
console.log('here we finally ve the file as a ArrayBuffer : ',temp);
const fileb = new Uint8Array(fileb)
... ... ...
}
where file
is directly the File
object u want to read , notice that this is an async
function...其中
file
直接是您要读取的File
对象,请注意这是一个async
函数...
I wish to read the contents of an upload file into a Javascript variable.我希望将上传文件的内容读入Javascript变量中。
The program used to work using file.getAsBinary but this is now deprecated and needs to be updated to use FileReader()该程序曾经使用file.getAsBinary进行工作,但是现在不建议使用,需要更新以使用FileReader()
A form has a file upload selection an onsubmit runs a function uploadDB() in a javascript.表单具有文件上传选择,onsubmit在javascript中运行函数uploadDB()。
The variable dbname is passed okay as is the file name from the upload selection I can see it with an alert or console.log.可以很好地传递变量dbname,就像上传选择中的文件名一样,我可以通过alert或console.log看到它。
The final bfile variable to receive the file text is undefined.接收文件文本的最终bfile变量未定义。 I have tried both readAsText and ReadAsBinary but bfile is undefined.
我已经尝试过readAsText和ReadAsBinary,但是bfile是未定义的。 The var declaration is in function uploadDB() and should be global to the inner function.
var声明位于函数uploadDB()中,并且对于内部函数应该是全局的。 If scope is somehow a problem how do I get the result in the bfile variable.
如果范围是一个问题,我如何在bfile变量中得到结果。
Its probably simple but I cannot get it to work.它可能很简单,但我无法使它正常工作。 Can someone suggest something please.
有人可以建议点什么。 The html section is;
html部分是;
<form onsubmit="uploadDB()"; method="post">
Database <input type=text name="dbName" id="dbName" size=20>
<input type=file id="metaDescFile" size=50 >
<input type=submit value="Submit">
</form>
The js script is similar to (extraneous stuff edited out); js脚本类似于(已删除多余的内容);
<script language="javascript" type="text/javascript">
<!--
var uploadDB = function() {
var input = document.getElementById('metaDescFile');
var fname = document.getElementById('metaDescFile').value;
var file=input.files[0];
var bfile;
reader = new FileReader();
reader.onload = function(e) { bfile = e.target.result }
reader.readAsText(file);
alert(bfile); // this shows bfile as undefined
// other stuff
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.