Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I open a file stream in javascript?

To give a concrete example of what I mean:

Imagine I have an HTML page that looks like this

<div>
   <div id="filecontents">
       <!-- some html file contents -->
   </div>
   <input type="button" />
</div>

I would like to be able to click on the button, and it bring me up with a "Open or save file" dialog box.

Is this even possible?

The objective of this is, is for me to be able to open up some html, text, or a CSV using the contents of a div as a data source for the file.

like image 880
tim Avatar asked Jul 19 '11 16:07

tim


People also ask

What is a file stream in JavaScript?

JavaScript Stream allows to programmatically access a stream of data received on the network. Streaming is to break the resource or data into smaller chunks, and processing is done bit by bit.

Can JavaScript read file system?

JavaScript does not have direct access to the local files due to security and privacy. We can offer the user the possibility to select files via a file input element that we can then process. The file input has a files property with the selected file(s).

Can JavaScript open local files?

Web browsers (and JavaScript) can only access local files with user permission. To standardize the file access from the browser, the W3C published the HTML5 File API in 2014. It defines how to access and upload local files with file objects in web applications.


1 Answers

You can do this using the HTML5 FileSystem APIs. A couple tutorials here:

  • Reading local files in JavaScript
  • Exploring the FileSystem APIs

Browser support is weak right now, but my guess is that it's the closest to what you want.

like image 116
Michael Mior Avatar answered Oct 04 '22 10:10

Michael Mior