Html html5 图片上传
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8890064/
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
html5 Image upload
提问by dualCore
I'm trying to make a VERY simple image uploader in html5.
我正在尝试在 html5 中制作一个非常简单的图片上传器。
<input type="file" multiple=""/>
All I would like to do is display what is uploaded without using PhP or anything. Could I use code similar to this?
我想要做的就是在不使用 PhP 或任何东西的情况下显示上传的内容。我可以使用类似的代码吗?
<img src="WHATEVER WAS UPLOADED"/>
Thanks!
谢谢!
回答by k1dbl4ck
i found
我发现
http://www.html5rocks.com/en/tutorials/file/dndfiles/
rather helpful.
比较有帮助。
if you dont want to push the image to some server ( i assume this from your question ), you can just update the image locally :
如果您不想将图像推送到某个服务器(我从您的问题中假设这一点),您可以只在本地更新图像:
<style>
.thumb {
height: 75px;
border: 1px solid #000;
margin: 10px 5px 0 0;
}
</style>
<input type="file" id="files" name="files[]" multiple />
<output id="list"></output>
<script>
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (!f.type.match('image.*')) {
continue;
}
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
// Render thumbnail.
var span = document.createElement('span');
span.innerHTML = ['<img class="thumb" src="', e.target.result,
'" title="', escape(theFile.name), '"/>'].join('');
document.getElementById('list').insertBefore(span, null);
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>
or somesuch.
或诸如此类。
回答by rasmusx
You could archive this with HTML5 File Api.
您可以使用 HTML5 File Api 将其存档。
Following tutorial should get you started: Reading local files in JavaScript
以下教程应该可以帮助您入门:在 JavaScript 中读取本地文件