Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在localstorage中对图像数组进行编码?_Javascript_Polymer_Vaadin - Fatal编程技术网

Javascript 如何在localstorage中对图像数组进行编码?

Javascript 如何在localstorage中对图像数组进行编码?,javascript,polymer,vaadin,Javascript,Polymer,Vaadin,如何在localstorage中对图像数组进行编码 我无法将映像数组(base64)放入本地存储。我想在输入中更新回(base64),以便将其更新到服务器中。请帮帮我,谢谢 ready(){ super.ready(); 此.$.fileUpload.addEventListener('change',(e)=>{ var filesToUpload=this.$.fileUpload; var files=filesToUpload.files; var maxFiles=files.le

如何在localstorage中对图像数组进行编码

我无法将映像数组(base64)放入本地存储。我想在输入中更新回(base64),以便将其更新到服务器中。请帮帮我,谢谢


ready(){
super.ready();
此.$.fileUpload.addEventListener('change',(e)=>{
var filesToUpload=this.$.fileUpload;
var files=filesToUpload.files;
var maxFiles=files.length;
var fd=新FormData();
if(FileReader&&files&&files.length){
对于(var i=0;i
我认为您应该移动
var arr=[]退出循环

我成功了! 我想和那些对此有看法的人分享一下

<input type="file" class="file-upload" id="fileUpload">
<vaadin-button on-tap="submit">Submit</vaadin-button>

submit(e) {

    e.preventDefault();

    var list = !!localStorage.getItem('imageData') ? JSON.parse(localStorage.getItem('imageData')) : [];
    var input = this.$.fileUpload;
    var files = input.files;

    var reader = new FileReader();
    reader.onload = function(e) {
        // localStorage["imageData"] = reader.result;
        list.push(reader.result);
        localStorage["imageData"] = JSON.stringify(list);
    };
    reader.readAsDataURL(files[0]);

提交
提交(e){
e、 预防默认值();
var list=!!localStorage.getItem('imageData')?JSON.parse(localStorage.getItem('imageData')):[];
var input=this.$.fileUpload;
var files=input.files;
var reader=new FileReader();
reader.onload=函数(e){
//localStorage[“imageData”]=reader.result;
list.push(reader.result);
localStorage[“imageData”]=JSON.stringify(列表);
};
reader.readAsDataURL(文件[0]);

}

旁注:这可能不是一个好主意,如果图像较大,则会在尺寸限制方面出现错误。改为尝试索引数据库,不确定,但您可能可以向其中写入更多数据。请参阅@jcubic,抱歉我这么晚才回复。我明白你的意见。我可以在每张照片中设置最大限制大小(1MB)。localstorage通常可以存储最少2 MB到10 MB的数据,这取决于所有平台。@tony19,我根据stackoverflow.com/a/19183658/6277151进行了测试,但它没有将数据存储在localstorage中。它在localstorage中有数据,但没有数组。@Lunlong没错
var arr=[]
应该在循环之外,您正在使用单个元素创建数组,因此LS总是使用最后一个值的数组。@jcubic,我不能这样做。能否确保显示如何在localstorage中存储图像阵列的代码?