9

I am working for a prototype of website(Only Browser based). There is a part where I need to upload some files.Here I am using JavaScript and HTML.

Whenever user wants to upload(Like Browse button in applications) some files then it will available for next time.I am unable to do this.

Question Can we save/store/upload a file using JavaScript/HTML in browser only(Not server )??

Thanks

3
  • 2
    I don't think it's possible to write files client-side only... you can read them via HTML5's File API, but writing them would probably cause way too many security issues Commented Oct 10, 2013 at 11:38
  • Is next time on the same machine only? Commented Oct 10, 2013 at 11:53
  • yes on the same machine. Is it possible. @ Michael Durrant Commented Oct 10, 2013 at 12:07

3 Answers 3

8

Downloading file directly to user's file-system

If you by save/store mean directly to user's computer/file system then no, this is not possible due to security reasons.

However, you can initiate a download which will pop up a "save as" type of requester and allow user to accept or deny to download the file.

There are more than one way to initiate a download. An interesting one is the new download attribute for the anchor tag where you can specify a file name as well as automatically initiate the download setting its href to a data-uri or something else you want to reference for download:

<a href="urlToFile" download="myFile.ext">Click to download</a>

Local storage mechanisms

If you simply want to save the file locally you can use one of the many local storage mechanisms such as:

Note that all of these as sand-boxed and only available in the browser using the same origin as they was written from. The data may or may not be discarded at any point as well (by user or by browser) so they are not a "safe" storage (always keep a server copy or a way to regenerate the data).

Sign up to request clarification or add additional context in comments.

4 Comments

for images which approach is best??
@ShyamDixit I would "recommend" File API but as it is in development and only supported by Chrome I would say Indexed DB as this allows Blob and you can request quota.
Please note that the statuses of each of the technologies are quite out of date, for example, the File System API is no longer being maintained.
@Harvey it seems like at least index DB's spec has been updated in 2018. Others indeed haven't been updated for 4+ years (or even 10 for Web SQL). So now in 2020, what are the recommended ways to store data on client side (browser)? It would be great if you can share as answer to this thread to make this post more updated.
2

Yes, it's possible via FileSystem API (currently only Chrome and Opera).

window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;

document.querySelector('input[type="file"]').onchange = function(e) {
  var files = this.files;

  window.requestFileSystem(window.TEMPORARY, 1024*1024, function(fs) {
    // Duplicate each file the user selected to the app's fs.
    for (var i = 0, file; file = files[i]; ++i) {

      // Capture current iteration's file in local scope for the getFile() callback.
      (function(f) {
        fs.root.getFile(f.name, {create: true, exclusive: true}, function(fileEntry) {
          fileEntry.createWriter(function(fileWriter) {
            fileWriter.write(f); // Note: write() can take a File or Blob object.
          }, errorHandler);
        }, errorHandler);
      })(file);

    }
  }, errorHandler);

};

4 Comments

"Whenever user wants to upload(Like Browse button in applications) some files then it will available for next time."
The files written will be stored in a sandboxed directory and are available for any time.
great! i was unaware of that.+1 But it's non standard way as mentioned here developer.mozilla.org/en-US/docs/Web/API/LocalFileSystem
You got the wrong link. FileSystem (not LocalFileSystem) is yet-to-be standardised.
-3

"Can we save/store/upload a file using JavaScript/HTML in browser only(Not server )??"

Ans. is No. If you want to retain the uploaded file then you need to store it on server.

The moment the browser tab is closed the file will get lost.

Alternative: What you can do is store the name of the file on server and whenever user request the file then ask him to upload it.

For uploading the file into memory you can refer to this link .

Live Demo

2 Comments

Yes I am able to save the file name only not the whole path
@ShyamDixit Then ask the user to enter the path manually during first upload.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.