IE6:
//img: image object //file: file inputbox object img.src = file.value;
IE7-8:
var src = ''; file.select(); try { src = document.selection.createRange().text; } finally { document.selection.empty(); } src = src.replace(/[)'"%]/g, function(s){ return escape(escape(s)); }); img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale',src=\"" + src + "\")";
Firefox 6以下:
img.src = file.files[0].getAsDataURL();
Firefox 3.6 (1.9.2)+,Chrome 7+,IE10+,Opera 11.1+,Safari Nightly build:
var oFile = file.files[0]; var oFReader = new FileReader(); oFReader.onload = function (oFREvent) { img.src = oFREvent.target.result; }; oFReader.readAsDataURL(oFile);
IE9:不能用IE8的方法,也不支持FileReader, http://html5labs.interoperabilitybridges.com/prototypes/fileapi/fileapi/info 里的FileAPI也不能用,需要进一步调查。
参考文档:
https://developer.mozilla.org/en/DOM/FileReader