2

Hi i want to save the array of an input type file.

<input type="file" id="fuGerber" onchange="saveFiles(this.files)" multiple />

I already tried this:

localStorage.setItem('files',JSON.stringify(files));

OR this:

 localStorage.setItem('files',JSON.stringify(files[0]));

but JSON.stringify returns empty "{}" or this in the second case "{"0":{}}" Does any one knows something to save it without lose nothing inside of the array.

This is what my array have:

This is what my array have

user3442776
  • 162
  • 2
  • 4
  • 13
  • Why would you like to store the data inside the array since its temporary? Would you not prefer the data of the image? –  Feb 08 '17 at 21:33
  • The file are not images, are text files and I need in that way because the second time the user select another file I have to refresh the page after its selection and when the page be loaded again I want to get that selection to in order to the user don't have to select again. – user3442776 Feb 08 '17 at 21:39
  • Just to be clear, files is not an array, it is a FileList. It is a subtle but important difference. https://developer.mozilla.org/en-US/docs/Web/API/FileList – mhodges Feb 08 '17 at 21:43
  • Possible duplicate of [How do I save and restore a File object in local storage](http://stackoverflow.com/questions/19119040/how-do-i-save-and-restore-a-file-object-in-local-storage) – mhodges Feb 08 '17 at 21:48
  • See also: http://stackoverflow.com/a/29281243/4987197 for a solution – mhodges Feb 08 '17 at 21:48
  • Is not the same issue because they have a pure JSON ({}). I have an array that contains JSON ([{}]) and i don't know is not working. And I'm not attempting to set the input file again. All this is because I have an issue with a js library that is not cleaning all its objects, so when I upload another file the application does not work fine, that's why I want to refresh the page automatically and save the selection of the users in order to they don't have to make a selection again after refreshing – user3442776 Feb 08 '17 at 21:53

2 Answers2

4

Maybe you're missing using a file reader to actually read the data from the file input which you can then store in an array before you save it to localStorage.

See this answer here.

Update:

So I finally realized what you were having trouble with after duplicating the issue in JSFiddle. The problem is FileList is NOT an array (see here), it's a custom list type. As a result the JSON encoder doesn't know how to encode properly.

To get the data from the FileList you have to manually create a file object of each File in the FileList, push each object to an array\, and then you will be able to stringify and set to localStorage.

Doing the above I was able to successfully save the files: View of files array in localStorage

See my code example:

JSFiddle

Cheers! I hope this helps :)

Community
  • 1
  • 1
Tammy Tee
  • 105
  • 1
  • 10
  • Not, I want to save the array before be processed for the fileReader. Basically save that input file have selected (array object). – user3442776 Feb 08 '17 at 21:41
  • 1
    That doesn't make sense. How else do you plan to get the data to actually save it? You still have to read the data in somehow otherwise it will result in the empty array you're receiving. @user3442776 – Tammy Tee Feb 08 '17 at 21:42
  • All this is because I have an issue with a js library that is not cleaning all its objects, so when I upload another file the application does not work fine, that's why I want to refresh the page automatically and save the selection of the users in order to they don't have to make a selection again after refreshing – user3442776 Feb 08 '17 at 21:46
  • 1
    What are you planning to use on the back-end of your application to store the files (node.js, php)? Or how were you planning to store them in general? – Tammy Tee Feb 08 '17 at 21:51
  • This files contains only coordinates (X,Y) and I display that coodinates in the UI with a canvas, in the back-end I'm using .Net web api, but I only save coordinates. – user3442776 Feb 08 '17 at 21:57
  • Hmmm ok. I'm assuming you're only allowing the user to upload one file at a time? If so, try accessing your data using `file` instead of `files`. Typically `files` is only true if the file input has the `multiple` attribute set like so: ``. – Tammy Tee Feb 08 '17 at 22:05
  • Not, users can load more than one file – user3442776 Feb 08 '17 at 22:08
-2

If you are trying save an array do this:

To save:

 localStorage.setItem('files', JSON.stringify(files));

To recovery from localStorage:

value  = localStorage.getItem('files');
result = JSON.parse(value);


Updated:

For a file check this link How to save an image to localStorage and display it on the next page?

Community
  • 1
  • 1
Samuel Teixeira
  • 326
  • 2
  • 9