How can I save the array of input type file in sto

2019-09-12 00:00发布

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

2条回答
相关推荐>>
2楼-- · 2019-09-12 00:24

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 :)

查看更多
我命由我不由天
3楼-- · 2019-09-12 00:30

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?

查看更多
登录 后发表回答