ASP.NET?MVC+EntityFramework圖片頭像上傳功能實(shí)現(xiàn)
1,先展示一下整體的效果
2,接下來展示用戶添加以及上傳頭像代碼、添加用戶界面
前端代碼如下:
<div class="form-group"> @Html.LabelFor(model => model.img, "頭像:", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @*@Html.EditorFor(model => model.img, new { htmlAttributes = new { @class = "form-control" } })*@ <input class="width-main input" type="file" datatype="*" id="pic" name="pic" value="" accept="image/*" onchange="upload(event)"> <input type="hidden" name="img" id="img" value="" /> <div id="showImg"></div> </div> </div>
JS代碼
<script> //實(shí)現(xiàn)異步上傳 function upload(event) { var imgPath = $("#pic").val(); console.log(imgPath); //判斷上傳文件的后綴名 var strExtension = imgPath.substr(imgPath.lastIndexOf('.') + 1); if (strExtension != 'jpg' && strExtension != 'gif' && strExtension != 'png' && strExtension != 'bmp') { alert("請選擇圖片文件"); return; } //實(shí)現(xiàn)文件上傳操作 if (event.target.files[0].type.search('image') !== -1) { //實(shí)現(xiàn)文件圖片的上傳 var formData = new FormData($("#myForm")[0]);//用于創(chuàng)建一個文件流對象 //formData.append('pic', $("#img")[0]); //添加文件流 (流名稱,流) //console.log(formData); $.ajax({ url: "/Upload/file", type: "post", cache: false, processData: false, contentType: false, data: formData, success: function (res) { console.log(res); if (res.trim() == "209") { alert("請選擇圖片!"); return; } if (res.trim() == "300") { alert("上傳的圖片不能為空圖片!"); return; } if (res.trim() == "400") { alert("上傳的圖片失?。?); return; } //alert("上傳成功!"); $("#showImg").html("<img src='" + res + "' width='50' height='50' /><p style='color:red;'>上傳成功!</p>"); //設(shè)置上傳的圖片地址 var res = res.trim(); //去除圖片的前后空白字符 $("#img").val(res); }, error: function (res) { alert("上傳異常!"); } }); } else { alert('只支持上傳圖片'); } } </script>
控制器圖片上傳的方法
//圖片上傳 [HttpPost] public ActionResult file(HttpPostedFileBase pic) { try { if (pic != null) { if (pic.ContentLength == 0) { return Content("209"); //獲取上傳的圖片 } else { //判斷文件的后綴名,是否符合條件 string backFix = Path.GetExtension(pic.FileName); if (backFix != ".gif" && backFix != ".png" && backFix != ".jpg" && backFix != ".jpeg") { return Content("210"); //格式不對 } string fileName = DateTime.Now.ToString("MMddHHmmss") + backFix; string strPath = Server.MapPath("~/Content/pic/" + fileName); pic.SaveAs(strPath); //返回路徑 return Content("/Content/pic/" + fileName); } } else { return Content("300"); //圖片不能為空 } } catch (Exception ) { return Content("400"); //上傳失敗 } }
數(shù)據(jù)庫保存的是文件的已經(jīng)重新命名的路徑,數(shù)據(jù)庫保存的圖片如下
在列表頁面如何具體顯示頭像呢,代碼如下所示:
以上就是頭像圖片的上傳展示,謝謝.
到此這篇關(guān)于ASP.NET MVC+EntityFramework圖片頭像上傳的文章就介紹到這了,更多相關(guān)ASP.NET MVC圖片頭像上傳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- ASP.NET?MVC+EntityFramework圖片頭像上傳功能實(shí)現(xiàn)
- ASP.NET MVC解決上傳圖片臟數(shù)據(jù)的方法
- ASP.NET?MVC實(shí)現(xiàn)單個圖片上傳、限制圖片格式與大小并在服務(wù)端裁剪圖片
- ASP.NET?MVC使用JCrop上傳并裁剪圖片
- Asp.net MVC使用swupload實(shí)現(xiàn)多圖片上傳功能
- ASP.NET MVC圖片上傳前預(yù)覽簡單實(shí)現(xiàn)
- ASP.NET MVC實(shí)現(xiàn)圖片上傳、圖片預(yù)覽顯示
- asp.net MVC實(shí)現(xiàn)無組件上傳圖片實(shí)例介紹
相關(guān)文章
Access數(shù)據(jù)庫中“所有記錄中均未找到搜索關(guān)鍵字”的解決方法
這個是Access一個天生不足的表現(xiàn),出現(xiàn)此錯誤是因?yàn)槟愕腁ccess數(shù)據(jù)庫有錯誤了。2008-08-08asp中獲取內(nèi)容中所有圖片與獲取內(nèi)容中第一個圖片的代碼
用asp獲取內(nèi)容中的圖片與獲取內(nèi)容中的第一個圖片地址,主要是cms中保存內(nèi)容中的圖片需要用得到,使用的是正則的方法。2011-01-01解決ASP中http狀態(tài)跳轉(zhuǎn)返回錯誤頁的問題
這篇文章主要介紹了ASP中http狀態(tài)跳轉(zhuǎn)返回錯誤頁的問題的解決方法,感興趣的小伙伴們可以參考一下2015-10-10asp 批量刪除選中的多條記錄的實(shí)現(xiàn)代碼
如果需要刪除記錄,一條一條的刪比較耗時,所以批量刪除記錄是個不錯的功能,原理就是利用asp數(shù)組來實(shí)現(xiàn)。2011-06-06轉(zhuǎn)換中文為unicode 轉(zhuǎn)換unicode到正常文本
轉(zhuǎn)換中文為unicode 轉(zhuǎn)換unicode到正常文本...2006-10-10