汗,修改 web/weui 文件夹下的 weui.me.js 文件,比如代码,黑色代码是新增的,用于判断文件大小
function previewFile() {
For (var i = 0, len = this.files.length; i < len; i++) {
var file = this.files[i];
alert("图片大小:" + file.size);
If (file.size > 1024 * 100) { // 100kb
alert("图片太大");
return;
}
}
如果,需要压缩,图片,就需要对图片进行一下处理,后,生成base64编码,如代码,红色为新增
switch(fln){
Case "jpg": case "jpeg":case "png":case "gif":case "bmp":case "wmf":
reader.onload= function(e){
var img = new Image();
img.onload = function () {
var w = Math.min(500, img.width); //宽度最大500
// 高度按比例计算
var h = img.height * (w / img.width);
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
// 设置 canvas 的宽度和高度
canvas.width = w;
canvas.height = h;
ctx.drawImage(img, 0, 0, w, h);
var base64 = canvas.toDataURL('image/png');
var nid =uid + "_thumb_" + cnt.toString()
thumbs.insertAdjacentHTML("beforeEnd","<li data-gid='"+ uid + "_gallery'" +
" data-image='" + nid + "'" + " id='" + nid + "'" +
" class='weui_uploader_file' temp style='background-image:url(" + base64 + ")'></li>");
images[images.length]="'"+ nid +"'";
glr.attributes["data-images"].value=images.join('|');
cnt = cnt + 1;
}
img.src = e.target.result;
}
reader.readAsDataURL(this.files[i]);
}