当前位置:Gxlcms > JavaScript > .net MVC+Bootstrap下使用localResizeIMG上传图片

.net MVC+Bootstrap下使用localResizeIMG上传图片

时间:2021-07-01 10:21:17 帮助过:2人阅读

本文实例为大家分享了使用localResizeIMG上传图片的具体代码,供大家参考,具体内容如下

需要加载的头文件

 html:

  1. <div class="form-group">
  2. <label class="col-sm-6 control-label" for="inputfile">维修照片上传</label>
  3. <div class="col-sm-6 ">
  4. <div style="background:url(../../fonts/add.png) no-repeat;width:151px;height:150px;float:left;" id="div1">
  5. <input type="file" accept="image/*" id="file" class="selectinput" style="width:151px;height:150px;opacity:.01">
  6. </div>
  7. </div>
  8. </div>

accept=“image/*”  这里有些手机可以拍照 有些不行 没有具体测试统计

  1. $("#file").localResizeIMG({
  2. width: 400,
  3. //height: 200,
  4. quality: 1,
  5. success: function (result) {
  6. var img = new Image();
  7. img.src = result.base64;
  8. //$("body").append(img);
  9. $("#odd").append(img); //呈现图像(拍照結果)
  10. $.ajax({
  11. url: "/Home/UploadImg",
  12. type: "POST",
  13. data: { "formFile": result.clearBase64, "RepairNum": $('#RepairNum').val()},
  14. dataType: "HTML",
  15. timeout: 1000,
  16. error: function () {
  17. alert("ajax Error");
  18. },
  19. success: function (data) {
  20. //alert("Uploads success~")
  21. }
  22. });
  23. }
  24. });

界面样式

后台action  Base64StringToImage 需要把压缩后的Base64转换

  1. [HttpPost]
  2. public ActionResult UploadImg()
  3. {
  4. var file = Request["formFile"];
  5. var id = Request["RepairNum"];
  6. string fileName = "1.jpeg";
  7. string filePath = Server.MapPath("~/Upload/" + fileName);
  8. try
  9. {
  10. Base64StringToImage(file, filePath);
  11. //upImg.SaveAs(filePhysicalPath);
  12. //Session["ImgPath"] = path;
  13. //Base64StringToImage(file,);
  14. return Content("上传成功");
  15. }
  16. catch
  17. {
  18. return Content("上传异常 !");
  19. }
  20. }
  21. protected void Base64StringToImage(string strbase64, string filepath)
  22. {
  23. try
  24. {
  25. byte[] arr = Convert.FromBase64String(strbase64);
  26. MemoryStream ms = new MemoryStream(arr);
  27. System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms);
  28. //bmp.Dispose();
  29. bmp.Save(filepath, System.Drawing.Imaging.ImageFormat.Jpeg);
  30. ms.Close();
  31. }
  32. catch (Exception ex)
  33. {
  34. }
  35. }

参考和下载GitHub:https://github.com/lyg945/localResizeIMG/tree/master/

参考文章:

localResizeIMG先压缩后使用ajax无刷新上传(移动端)

移动端使用localResizeIMG4压缩图片

基于javascript html5实现多文件上传

JS实现异步上传压缩图片

spring mvc+localResizeIMG实现HTML5端图片压缩上传

HTML5+Canvas调用手机拍照功能实现图片上传(上)

HTML5实现微信拍摄上传照片功能 遇到问题的解决方法

js实现纯前端的图片预览

推荐三款不错的图片压缩上传插件(webuploader、localResizeIMG4、LUploader)

H5图片压缩与上传实例

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

人气教程排行