时间:2021-08-15 09:33:46 | 栏目:JavaScript代码 | 点击:次
本文实例为大家分享了使用localResizeIMG上传图片的具体代码,供大家参考,具体内容如下
需要加载的头文件
html:
<div class="form-group"> <label class="col-sm-6 control-label" for="inputfile">维修照片上传</label> <div class="col-sm-6 "> <div style="background:url(../../fonts/add.png) no-repeat;width:151px;height:150px;float:left;" id="div1"> <input type="file" accept="image/*" id="file" class="selectinput" style="width:151px;height:150px;opacity:.01"> </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); //呈现图像(拍照?Y果) $.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端图片压缩上传