JS函数实现部分
/*
* 绑定文件域onchange事件,实现图片上传前预览 IE6-IE10 firefox Chrome
* @param fileInputObject Object onchange中传this执行input
* @param imgControlsID string image标签id
* @return void
*/
function changePreviewImgSrc(fileInputObject,imgControlsID)
{
if(typeof FileReader === "undefined")
{
//针对各个版本的IE
if(navigator.userAgent.indexOf("MSIE") > 0)
{
document.getElementById("imgControlsID").src = "file:///" + fileInputObject.value;
}
}
else
{
var reader = new FileReader();
reader.readAsDataURL(fileInputObject.files[0]);
reader.onload = function(e)
{
document.getElementById("imgControlsID").src = e.target.result;
}
}
}
HTML demo
<input type="file" οnchange="changePreviewImgSrc(this,'ff')"/>
<img id="ff" />