How to read a local text file?

前端 未结 20 2255
北恋
北恋 2020-11-21 05:28

I’m trying to write a simple text file reader by creating a function that takes in the file’s path and converts each line of text into a char array, but it’s not working.

20条回答
  •  野性不改
    2020-11-21 05:58

    Modern solution:

    Use fileOrBlob.text() as follows:

    
    

    When user uploads a text file via that input, it will be logged to the console. Here's a working jsbin demo.

    Here's a more verbose version:

    
    
    

    Currently (January 2020) this only works in Chrome and Firefox, check here for compatibility if you're reading this in the future: https://developer.mozilla.org/en-US/docs/Web/API/Blob/text

    On older browsers, this should work:

    
    
    

    Related: As of September 2020 the new Native File System API available in Chrome and Edge in case you want permanent read-access (and even write access) to the user-selected file.

提交回复
热议问题