需要加载的头文件
html:
<div> <label for="inputfile">维修照片上传</label> <div> <div> <input type="file" accept="image/*"> </div> </div> </div>
accept=“image/*” 这里有些手机可以拍照 有些不行 没有具体测试统计
$("#file").localResizeIMG({ width: 400, //height: 200, quality: 1, success: function (result) { var img = new Image(); img.src = result.base64; //$("body").append(img); $("#odd").append(img); //呈现图像(拍照結果) $.ajax({ url: "/Home/UploadImg", type: "POST", data: { "formFile": result.clearBase64, "RepairNum": $('#RepairNum').val()}, dataType: "HTML", timeout: 1000, error: function () { alert("ajax Error"); }, success: function (data) { //alert("Uploads success~") } }); } });
界面样式
后台action Base64StringToImage 需要把压缩后的Base64转换
[HttpPost] public ActionResult UploadImg() { var file = Request["formFile"]; var id = Request["RepairNum"]; string fileName = "1.jpeg"; string filePath = Server.MapPath("~/Upload/" + fileName); try { Base64StringToImage(file, filePath); //upImg.SaveAs(filePhysicalPath); //Session["ImgPath"] = path; //Base64StringToImage(file,); return Content("上传成功"); } catch { return Content("上传异常 !"); } } protected void Base64StringToImage(string strbase64, string filepath) { try { byte[] arr = Convert.FromBase64String(strbase64); MemoryStream ms = new MemoryStream(arr); System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms); //bmp.Dispose(); bmp.Save(filepath, System.Drawing.Imaging.ImageFormat.Jpeg); ms.Close(); } catch (Exception ex) { } }
参考和下载GitHub:https://github.com/lyg945/localResizeIMG/tree/master/
参考文章:
localResizeIMG先压缩后使用ajax无刷新上传(移动端)
spring mvc+localResizeIMG实现HTML5端图片压缩上传