Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/401.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.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 如何在会话存储或本地存储中存储文件(txt、pdf等)?_Javascript_Html_Jquery_Local Storage_Session Storage - Fatal编程技术网

Javascript 如何在会话存储或本地存储中存储文件(txt、pdf等)?

Javascript 如何在会话存储或本地存储中存储文件(txt、pdf等)?,javascript,html,jquery,local-storage,session-storage,Javascript,Html,Jquery,Local Storage,Session Storage,我有一个简单的文件输入框和一个按钮。我试图在点击按钮时将文件存储在会话中,这样,如果页面被刷新,它将处于会话中。 我试着用 函数storeInSession(){ 元素=document.getElementById('附件') var file=element.files[0]; sessionStorage.setItem('fileInSession',file) sessionFile=sessionStorage.getItem('fileInSession') } 上传存储图像

我有一个简单的文件输入框和一个按钮。我试图在点击按钮时将文件存储在会话中,这样,如果页面被刷新,它将处于会话中。 我试着用

函数storeInSession(){
元素=document.getElementById('附件')
var file=element.files[0];
sessionStorage.setItem('fileInSession',file)
sessionFile=sessionStorage.getItem('fileInSession')
}


上传存储图像

这里的想法是能够获取已加载到当前网页中的图像,并将其存储到localStorage中。如上所述,localStorage只支持字符串,因此我们需要在这里将图像转换为数据URL。对图像执行此操作的一种方法是加载到画布元素中。然后,使用画布,可以将画布中的当前视觉表示作为数据URL读取

让我们看看这个例子,文档中有一个id为“大象”的图像:

// Get a reference to the image element
var elephant = document.getElementById("elephant");

// Take action when the image has loaded
elephant.addEventListener("load", function () {
    var imgCanvas = document.createElement("canvas"),
        imgContext = imgCanvas.getContext("2d");

    // Make sure canvas is as big as the picture
    imgCanvas.width = elephant.width;
    imgCanvas.height = elephant.height;

    // Draw image into canvas element
    imgContext.drawImage(elephant, 0, 0, elephant.width, elephant.height);

    // Get canvas contents as a data URL
    var imgAsDataURL = imgCanvas.toDataURL("image/png");

    // Save image into localStorage
    try {
        localStorage.setItem("elephant", imgAsDataURL);
    }
    catch (e) {
        console.log("Storage failed: " + e);
    }
}, false); 

您可以将二进制数据编码为格式,并将其用作字符串

对不起,您是说这不可能,但仍在询问如何执行?您当前的错误是什么?