應用場景:MUI+H5+WEBAPI
今天在給我外甥女調手機端上傳圖片的時候,發現他是直接調用的MUI下的api,直接調取相冊或者相機,到最后只看見了一個文件的路徑,所以以前寫的上傳文件就不太好套上去了,我又比較懶(`・ω・´),我就去查MUI的api,功夫不負有心人讓我找到了MUI下用來專門處理上傳的方法,我也就不賣弄文章了,畢竟我搞前端也是很讓人捉急,獻丑了
js代碼:
1 var task = plus.uploader.createUpload( "http://47.94.245.189:807/abldoctor/upload/upload", 2 { method:"POST",blocksize:204800,priority:100 }, 3 function ( t, status ) { 4 // 上傳完成 5 console.log(t); 6 if ( status == 200 ) { 7 alert( "Upload success: " + t.url ); 8 } else { 9 alert( "Upload failed: " + status ); 10 } 11 } 12 ); 13 task.addFile('images/touxiang2x.png', {key:"file"} ); 14 task.start();
接口代碼:
1 /// <summary> 2 /// 上傳文件 3 /// </summary> 4 /// <returns></returns> 5 [HttpPost] 6 public object Upload() 7 { 8 int l = HttpContext.Current.Request.Files["file"].ContentLength; 9 byte[] buffer = new byte[l]; 10 Stream s = HttpContext.Current.Request.Files["file"].InputStream; 11 System.Drawing.Bitmap image = new System.Drawing.Bitmap(s); 12 string imgname = System.Guid.NewGuid().ToString() + ".jpg"; 13 string path = "Images/" + DateTime.Now.ToString("yyyyMMdd") + "/"; 14 if (!Directory.Exists(HttpContext.Current.Server.MapPath(path))) 15 { 16 System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(path)); 17 } 18 image.Save(HttpContext.Current.Server.MapPath(path + "/" + imgname)); 19 return new Content.ResultHelper().Results(new { bRes = true, filename = imgname, filePath = path }, Content.rspCodeNum.right, ""); 20 }
這就是全部了,大家如果有好的改進的建議,可以交流一下,畢竟活到老學到老嘛!!!
