溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

.net MVC+Bootstrap下使用localResizeIMG上傳圖片

發(fā)布時間:2020-09-05 09:27:33 來源:腳本之家 閱讀:161 作者:sj2016 欄目:web開發(fā)

本文實例為大家分享了使用localResizeIMG上傳圖片的具體代碼,供大家參考,具體內(nèi)容如下

需要加載的頭文件

.net MVC+Bootstrap下使用localResizeIMG上傳圖片

 html:

<div class="form-group">
 <label class="col-sm-6 control-label" for="inputfile">維修照片上傳</label>
 <div class="col-sm-6 ">
 <div  id="div1">
 <input type="file" accept="image/*" id="file" class="selectinput" >
 </div>

 </div>
 </div>

accept=“image/*”  這里有些手機可以拍照 有些不行 沒有具體測試統(tǒng)計

$("#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); //呈現(xiàn)圖像(拍照結(jié)果)
 $.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~")
 }
 });
 }
 });

界面樣式

.net MVC+Bootstrap下使用localResizeIMG上傳圖片

后臺action  Base64StringToImage 需要把壓縮后的Base64轉(zhuǎn)換

[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無刷新上傳(移動端)

移動端使用localResizeIMG4壓縮圖片

基于javascript html5實現(xiàn)多文件上傳

JS實現(xiàn)異步上傳壓縮圖片

spring mvc+localResizeIMG實現(xiàn)HTML5端圖片壓縮上傳

HTML5+Canvas調(diào)用手機拍照功能實現(xiàn)圖片上傳(上)

HTML5實現(xiàn)微信拍攝上傳照片功能 遇到問題的解決方法

js實現(xiàn)純前端的圖片預覽

推薦三款不錯的圖片壓縮上傳插件(webuploader、localResizeIMG4、LUploader)

H5圖片壓縮與上傳實例

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI