HTML5 File API: How to see the result of readAsText()

后端 未结 4 733
野趣味
野趣味 2020-12-08 04:30

When readAsText() function is completed the result is stored in .result

How do I see if the content of the file read are correct in .

4条回答
  •  被撕碎了的回忆
    2020-12-08 05:11

    This is a complete html and vanilla javascript example that creates a simple file input and a file reader that reads the file with FileReader.readAsText()then writes the text content of that file to the console. This works well for files like .txt or .csv.

    There are also FileReader.readAsArrayBuffer(), FileReader.readAsBinaryString(), and FileReader.readAsDataURL() which might work better for other use cases. I also recommend reading https://developer.mozilla.org/en-US/docs/Web/API/FileReader

    Note: Users can select multiple files to include in the input, this code will only read the first of those files (as you can see in the reference to the [0] element in event.target.files.

    
      
        
      
      
        
      
    
    

提交回复
热议问题