手機端圖片有很大的,上傳的時候很慢,這時候就要壓縮一下了,有一個開源的js能夠壓縮圖片的大小,開源地址以下:https://github.com/think2011/localResizeIMG3javascript
代碼以下:html
// 上傳圖片 function uploadFile(path) { var type = plus.storage.getItem("upload_goods_image"); //選擇成功 $("#heisebg").removeClass("heisebg").addClass("heisebghid"); $("#waitingupload").removeClass("heisebghid").addClass("heisebg"); //https://github.com/think2011/localResizeIMG3 // 壓縮圖片 lrz(path, { width: 500, quality: 0.7, done: function (results) { $.ajax({ type: "POST", url: configManager.RequstUrl + "api/common/base64upload", async: true, data: { base64: results.base64, size: results.base64.length, dir: "goods" } }).done(function (data) { if (data.state != "success") { console.log(data.message); return; } var src = configManager.goodsImgurl.format(data.id, "") + '500-200'; if ("addspic" == type) { var sImageStr = "<img width='98%' onclick='javascript:$(this).remove();' class='spic' title='{0}' src='{1}'>"; $("#addspic").before(sImageStr.format(data.id, src)); } if ("addbpic" == type) { var bImageStr = "<img width='98%' id='bpic' title='{0}' src='{1}'/>"; $("#addbpic").html(bImageStr.format(data.id, src)); } $("#waitingupload").removeClass("heisebg").addClass("heisebghid"); try { myScroll.refresh(); } catch (err) { } }).fail(function () { plus.nativeUI.toast("上傳失敗!"); $("#waitingupload").removeClass("heisebg").addClass("heisebghid"); }); } }); }