简体   繁体   English

如何在sessionStorage中保存输入文件类型信息?

[英]How to save input file type information in sessionStorage?

I have this code for file saving browsing, specifically image.. 我有这段代码用于文件保存浏览,特别是图像。

I'm aware that you can't set file programmatically, you have to go to folder popups in order to select file for security reason. 我知道您不能以编程方式设置文件,出于安全原因,您必须转到文件夹弹出窗口才能选择文件。 So my problem is, I want the file to be saved in sessionStorage so that when the users reload the page, they don't need to reselect the image. 所以我的问题是,我希望将文件保存在sessionStorage中,以便用户重新加载页面时,他们不需要重新选择图像。

I'm doing this approach.. 我正在做这种方法..

var fileInput = document.getElementById('file');
sessionStorage.input = JSON.stringify(fileInput);

But when I reload the page, it's not working anymore with.. 但是,当我重新加载页面时,它不再起作用。

formData.append(fileInput.files[0].name, sessionStorage.input);

I tried to do console.log(sessionStorage.inputFile) but it only printed '[object File]' 我试图做console.log(sessionStorage.inputFile),但它只打印'[object File]'

You can use below code to save image data in session storage 您可以使用以下代码将图像数据保存在会话存储中

var file = element.files[0];
var reader = new FileReader()
reader.onload = function(base64) {
    localStorage["file"] = base64;
}

//Store it in session storage
var img_base64 = localStorage["file"];

var base64_arr = img_base64_data.split(",");
var img_format = base64_arr[0].split(";")[1];
var img_content = base64_arr[1];

var file = new File([img_content], "<file name>", {type: img_format});

// get file as file.files[0] and store it in session storage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM