简体   繁体   English

fileReader.readAsText()JavaScript:未显示结果

[英]fileReader.readAsText() javascript: result not displayed

I am working on a html project in which the user uploads a txt file whose content is further processed and the result shown. 我正在处理一个html项目,其中用户上传了一个txt文件,其内容将得到进一步处理并显示结果。 The upload and processing step works fine but somehow the result is never shown. 上载和处理步骤工作正常,但从某种程度上讲,结果从未显示出来。 The (for me) confusing part is that if I add an alert() between processing and result presentation, the result is then displayed correctly. (对我而言)令人困惑的部分是,如果我在处理和结果表示之间添加了alert(),则结果将正确显示。

Is this a known issue that I am missing? 这是我所缺少的已知问题吗?

In the code example bellow, if you remove the commentary on the alert(), the result is displayed correctly in the "demo" paragraph. 在下面的代码示例中,如果删除了alert()上的注释,则结果将在“演示”段落中正确显示。 But without it, nothing is shown. 但是没有它,什么也没显示。

I'd be grateful for either an explanation of my mistake or an alternative solution. 对于我对错误的解释或替代解决方案,我将不胜感激。

Thank you very much! 非常感谢你!

 function myFunction() { var x = document.getElementById("file"); var textFromFileLoaded = ""; if (x.value != "") { // text upload var file = x.files[0]; var fileReader = new FileReader(); fileReader.onload = function(fileLoadedEvent) { textFromFileLoaded = fileLoadedEvent.target.result; } fileReader.readAsText(file, "UTF-8"); } //alert("somth"); //HERE document.getElementById("demo").innerHTML = textFromFileLoaded; return true; } 
 Select a file to upload: <input type="file" id="file" size="50" accept="image/*"> <button onclick="myFunction()">button</button> <p id="demo"></p> 

You should put document.getElementById("demo").innerHTML = textFromFileLoaded; 您应该将document.getElementById("demo").innerHTML = textFromFileLoaded; inside the onload function 在onload函数中

fileReader.onload = function(fileLoadedEvent) {
  textFromFileLoaded = fileLoadedEvent.target.result;
  document.getElementById("demo").innerHTML = textFromFileLoaded;
}

Filereader is asynchronous so when you add the alert you give your code enough time to call onload before you add the text into the demo element. Filereader是异步的,因此当您添加警报时,在将文本添加到demo元素之前,您有足够的时间调用代码onload。 Without the alert textFromFileLoaded is not set (from the onload function) when you add it to the demo element 当您将其添加到演示元素中时,没有设置警告textFromFileLoaded (通过onload函数)

暂无
暂无

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

相关问题 将FileReader.readAsText()结果字符串转换回文件对象以供下载? - Convert FileReader.readAsText() result string back into a file object for download? 如何在 JavaScript 中同步使用 FileReader.readAsText 读取文件? - How to read file using FileReader.readAsText synchronously in JavaScript? 使用 FileReader.readAsText() 将 a.txt 文件拆分为 JavaScript 数组 - Splitting a .txt file into a JavaScript Array using FileReader.readAsText() fileReader.readAsText 返回一个带引号的字符串 - fileReader.readAsText returns a string with quote 抛出&#39;FileReader&#39;的fileReader.readAsText():参数1不是&#39;Blob&#39;类型 - fileReader.readAsText() throwing 'FileReader': parameter 1 is not of type 'Blob' HTML5 File API 中的 FileReader.readAsText 如何工作? - How FileReader.readAsText in HTML5 File API works? HTML5 文件 API:FileReader.readAsText() 返回“未定义” - HTML5 File API: FileReader.readAsText() returns "undefined" 来自“ HTML5 File API中的FIleReader.readAsText如何工作”的FileReader错误 - FileReader Error from “How FIleReader.readAsText in HTML5 File API works” Cordova 文件插件 FileReader.readAsText 获取错误但不调用错误回调 - Cordova File plugin FileReader.readAsText gets error but does not call error callback JavaScript FileReader中的readAsDataURL()和readAsArrayBuffer()以及readAsText()之间的区别 - Difference between readAsDataURL() and readAsArrayBuffer() and readAsText() in JavaScript FileReader
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM