图片预览
/*
* @param {string} fileId input标签id
* @param {string} imageId img标签id
* @param {number} maxSize 图片大小
*/
function imagePreview(fileId, imageId, maxSize) {
var maxSize = maxSize || 1024 * 1024;
var oFile = document.getElementById(fileId).files[0];
var oImage = document.getElementById(imageId);
var rFilter = /^(image/bmp|image/gif|image/jpeg|image/png|image/tiff)$/i;
if (!oFile) {
oImage.src = "";
return;
}
if (!rFilter.test(oFile.type)) {
alert("请选择图片");
return;
}
if (oFile.size > maxSize) {
alert("图片太大");
return;
}
var oReader = new FileReader();
oReader.onload = function(e) {
oImage.src = e.target.result;
};
oReader.readAsDataURL(oFile);
}