javascript JS - 保存上传的文件做光盘

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29496603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:37:50  来源:igfitidea点击:

JS - saving uploaded file do disc

javascripthtml

提问by Alexander Nikolov

I have this file input

我有这个文件输入

<input type="file" class="file-input">

which I am using to upload images in my project.

我用来在我的项目中上传图像。

How can I save the uploaded image to certain local folder with JS ?

如何使用 JS 将上传的图像保存到某个本地文件夹?

回答by Touhid Alam

If you are willing to save the files locally, you can use the HTML5 LocalStoragefeature. Its a fairly simple technology to store key/value pairs inside the browser storage. As MDNsuggests, I prefer using the Web Storage API. For storing the image, a solution can be:

如果您愿意将文件保存在本地,则可以使用 HTML5 LocalStorage功能。它是一种将键/值对存储在浏览器存储中的相当简单的技术。正如MDN 所建议的,我更喜欢使用 Web Storage API。为了存储图像,解决方案可以是:

$('input[type="file"]').on('change', function () {
    var reader = new FileReader();
    reader.onload = function () {
        var thisImage = reader.result;
        localStorage.setItem("imgData", thisImage);
    };
    reader.readAsDataURL(this.files[0]);
});

Here's a demo storing an image from an element, storing the image inside the local storage and showing it back. http://jsfiddle.net/touhid91/nmy2b9j4/

这是一个从元素存储图像的演示,将图像存储在本地存储中并显示出来。 http://jsfiddle.net/touhid91/nmy2b9j4/