上傳前顯示圖像預覽在我的HTML表單中,我使用類型文件進行輸入,例如: <input type="file" multiple>然后單擊輸入按鈕選擇多個文件?,F在我想在提交表單之前顯示選定圖像的預覽。如何在HTML 5中做到這一點?
3 回答
慕容3067478
TA貢獻1773條經驗 獲得超3個贊
FileReadersrc
<input type="file" id="files" /><img id="image" />
document.getElementById("files").onchange = function () {
var reader = new FileReader();
reader.onload = function (e) {
// get loaded data and render thumbnail.
document.getElementById("image").src = e.target.result;
};
// read the image file as a data URL.
reader.readAsDataURL(this.files[0]);};function handleFileSelect(evt) {
var files = evt.target.files;
// 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 style="height: 75px; border: 1px solid #000; margin: 5px" 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);<input type="file" id="files" multiple /><output id="list"></output>
- 3 回答
- 0 關注
- 449 瀏覽
相關問題推薦
添加回答
舉報
0/150
提交
取消
