【发布时间】:2011-07-09 04:16:55
【问题描述】:
我正在尝试修改示例http://www.html5rocks.com/en/tutorials/file/dndfiles/#toc-reading-files 以使函数handleFileSelect(evt) 返回reader.result;我的意思是让函数返回base64的图像左右。 我试图用函数编写它,但它只返回 null :( 所以我的问题是如何让函数返回base64?
至于现在我试着写这个sn-p...
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="', theFile.name, '"/>'].join('');
document.getElementById('list').insertBefore(span, null);
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
return reader.result;
}
感谢所有有用的 cmets :)
【问题讨论】:
-
接受答案的19个问题?
-
请贴出问题的代码。
标签: javascript fileapi