应用场景: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 }
这就是全部了,大家如果有好的改进的建议,可以交流一下,毕竟活到老学到老嘛!!!