如何将输入类型文件的数组保存在存储位置? [英] How can I save the array of input type file in storage location?
问题描述
< input type =fileid = fuGerberonchange =saveFiles(this.files)multiple />
我已经试过了这个:
localStorage.setItem( '文件',JSON.stringify(文件));
或者:
localStorage.setItem('files',JSON.stringify(files [0]));
但是JSON.stringify返回空{},或者在第二种情况下返回{0 :{}}
是否有人知道要保存它,而不会在数组里面丢失任何东西。
这就是我的数组:
>
也许你错过了使用文件读取器从文件输入中读取数据,然后将其保存到数组中,然后将其保存到 localStorage
。
看到这个答案这里 a>。 更新: 所以我终于意识到你在做什么在JSFiddle中复制问题后出现问题。问题是FileList不是一个数组(看到这里),它是一个自定义列表类型。因此,JSON编码器不知道如何正确编码。 要从FileList获取数据,您必须手动创建FileList中每个文件的文件对象,将每个对象推送到一个数组,然后能够字符串化并设置为localStorage。 完成上述操作,我能够成功保存文件: 查看我的代码示例: 干杯!我希望这有助于:) Hi i want to save the array of an input type file. I already tried this: OR this: 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: 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 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: Cheers! I hope this helps :) 这篇关于如何将输入类型文件的数组保存在存储位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
在localStorage中查看文件数组
<input type="file" id="fuGerber" onchange="saveFiles(this.files)" multiple />
localStorage.setItem('files',JSON.stringify(files));
localStorage.setItem('files',JSON.stringify(files[0]));
localStorage
.