使用FileReader我不懂的两个函数

时间:2013-01-19 23:51:21

标签: javascript html5

我在理解(function(theFile) { return function(e) {如何运作方面遇到了问题。

该文件的作用是什么,如果返回e,它如何用作e.target.result

})(f);做了什么,为什么它在函数之外。

// Check for the various File API support.
if (window.File && window.FileReader && window.FileList && window.Blob) {
    // Great success! All the File APIs are supported.
} else {
    alert('The File APIs are not fully supported in this browser.');
}

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), '" id="', escape(theFile.name), '" onclick="_Element=this.id, RefreshElement()" /><br>'].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);

0 个答案:

没有答案