Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
previewImage(file, callback) {//file为plupload事件监听函数参数中的file对象,callback为预览图片准备完成的回调函数
if (!file || !/image\//.test(file.type)) return; //确保文件是图片
if (file.type === 'image/gif') {//gif使用FileReader进行预览,因为mOxie.Image只支持jpg和png
let fr = new plupload.mOxie.FileReader();
// console.log(fr);
fr.onload = function () {
callback(fr.result);
fr.destroy();
fr = null;
};
fr.readAsDataURL(file.getSource());
} else {
let preloader = new plupload.mOxie.Image();
preloader.onload = function () {
preloader.downsize(300, 300);//先压缩一下要预览的图片,宽300,高300
let imgsrc = preloader.type === 'image/jpeg' ? preloader.getAsDataURL('image/jpeg', 80) : preloader.getAsDataURL(); //得到图片src,实质为一个base64编码的数据
callback && callback(imgsrc); //callback传入的参数为预览图片的url
preloader.destroy();
preloader = null;
};