1.前端頁面代碼 /** * 經過圖片本地路徑獲取圖片真實大小,並進行壓縮 */ function getLocalRealSize(path, callback) { var img = new Image(); var tempimg = new Image(); img.src = path; img.onload = function() { var height = img.naturalHeight; var width = img.naturalWidth; img.width = img.naturalWidth; img.height = img.naturalHeight; //壓縮圖片 var canvas = compressImage(img); tempimg = canvas.toDataURL('image/jpeg'); callback(tempimg); } }; /** * 壓縮圖片 */ function compressImage(image) { var canvas = document.createElement("canvas"); var mwidth = image.width; var mheight = image.height; var scale = 1; var i = 0; while (true) { if (mwidth >> i <= 1000 && mheight >> i <= 1000) { scale = Math.pow(0.5, i); break; } i += 1; } canvas.width = mwidth * scale; canvas.height = mheight * scale; canvas.getContext("2d").drawImage(image, 0, 0, mwidth * scale, mheight * scale); return canvas; } //主頁面 車主首頁 跳轉 document.getElementById("driverindex").addEventListener("tap", function() { //獲取img對象 var OwerImg = document.getElementById("owner-photo-img"); var OwerCardImg = document.getElementById("owner-cardphoto-img"); var OwerLicenceImg = document.getElementById("owner-driver1-img"); var OwerDrivingLicenceImg = document.getElementById("owner-driver2-img"); var CarImg = document.getElementById("owner-car-img"); var AssuranceImg = document.getElementById("owner-sure-img"); var oArray=source.split("|"); var totalCount=imgCount+oArray.length-1; if(totalCount<6){ mui.toast("請上傳完整信息"); return false; } uploadImg(); }); }); // 上傳文件 var uploadImg = function() { var server = imgUrl+'/clientapi/driver/ApplayCertification'; //outSet("開始上傳:") var wt = plus.nativeUI.showWaiting(); var task = plus.uploader.createUpload(server, { method: "POST", blocksize: 20480000, priority: 100 }, function(t, status) { //上傳完成 if (t.state == 4 && status == 200) { wt.close(); //outLine("上傳成功:" + t.responseText); //上傳成功,跳轉到車主首頁 mui.toast('上傳成功'); mui.fire(mycarPage, "pageflowrefresh" ); plus.webview.currentWebview().close(); mui.redirect('myaccount-mycar', '../myaccount/mycar.html'); } else { //outLine("上傳失敗:" + status); wt.close(); mui.toast('上傳失敗'); } } ); task.addData("token", UserInfo.token()); task.addData("source", source); task.start(); } 2.C#後臺代碼 //將base64編碼的字符串轉爲圖片並保存 protected string SaveBase64Image(string source, string path, int userID) { try { var now = DateTime.Now; string filePath = path + now.ToString("yyyy-MM-dd") + "/" + userID.ToString() + "/"; string fileName = now.ToString("yyyyMMddhhmmssffff") + ".jpg"; Log.Debug("filePath:" + filePath + fileName); string strbase64 = source.Substring(source.IndexOf(',') + 1); strbase64 = strbase64.Trim('\0'); //Log.Debug("strbase64:" + strbase64); byte[] arr = Convert.FromBase64String(strbase64); using (MemoryStream ms = new MemoryStream(arr)) { Log.Debug("進入了MemoryStream"); Bitmap bmp = new Bitmap(ms); if (!Directory.Exists(Server.MapPath(filePath))) { Log.Debug("沒有Directory"); Directory.CreateDirectory(Server.MapPath(filePath)); } //if (!Directory.Exists(filePath)) // Log.Debug("沒有Directory"); //Directory.CreateDirectory(filePath); //新建第二個bitmap類型的bmp2變量。 Bitmap bmp2 = new Bitmap(bmp, bmp.Width, bmp.Height); //將第一個bmp拷貝到bmp2中 Graphics draw = Graphics.FromImage(bmp2); draw.DrawImage(bmp, 0, 0); draw.Dispose(); Log.Debug("保存圖片前"); bmp2.Save(Server.MapPath(filePath + fileName), System.Drawing.Imaging.ImageFormat.Jpeg); Log.Debug("保存圖片後"); //bmp.Save("test.bmp", ImageFormat.Bmp); //bmp.Save("test.gif", ImageFormat.Gif); //bmp.Save("test.png", ImageFormat.Png); ms.Close(); return filePath + fileName; } } catch (Exception ex) { return null; } }