简体   繁体   中英

Javascript to load file

I want user upon clicking a button it will load the a txt file from a eg Desktop and display the contents.

Can this be done?

I found a lot of examples for loading a file but not displaying it.

JavaScript in the browser can't access the user's file system - it would be a security issue.

  1. Consider other client side options like Java Applets or Flash.
  2. In the worst-case, you could upload the file to the server and send it back to the client - but there's probably no usecase that calls for such an expensive hack.

Additionally, check out this SO question - the File API will allow this.

There's an excellent tutorial on html5rocks.com, 'Reading Files in JavaScript using the File APIs', that shows how to use the FileReader interface:

http://www.html5rocks.com/en/tutorials/file/dndfiles/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM