与这里的其他答案相反,如果您使用现代浏览器,您可以获取并存储相当多的文件内容<input> using elm.files
, FileReader and window.localStorage。你甚至可以告诉浏览器再次保存(默认下载行为).
需要注意的是,这样做does not意味着你可以设置.value
on the <input>
node.
这是您可以执行的操作的示例,假设有一个文件已被选择.
// From <input> node
var elm = document.getElementById('image'),
img = elm.files[0],
fileName = img.name, // not path
fileSize = img.size; // bytes
// By Parsing File
var reader = new FileReader(),
binary, base64;
reader.addEventListener('loadend', function () {
binary = reader.result; // binary data (stored as string), unsafe for most actions
base64 = btoa(binary); // base64 data, safer but takes up more memory
}, false);
reader.readAsBinaryString(img);
从这里您可以保存在localStorage
, 创造dataURIs等等。例如,说来自fileName
我们知道图像是.jpg,然后你可以通过设置来显示它<img>
's src to "data:image/jpeg;base64," + base64
.
请注意,对此数据的任何修改不会对所选的原始文件产生任何影响。