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)建一個(gè)文件流對(duì)象
//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"); //格式不對(duì)
}
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)單個(gè)圖片上傳、限制圖片格式與大小并在服務(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)文章
asp 刪除數(shù)據(jù)并同時(shí)刪除圖片的代碼
實(shí)現(xiàn)刪除記錄同時(shí)刪除圖片,一般的情況為了性能都是用編輯器的同時(shí)將文章中的圖片,保存在一個(gè)字段里面,多個(gè)圖片用逗號(hào)分開,方便后期的刪除處理。2010-05-05
Microsoft JET Database Engine(0x80004005)未指定錯(cuò)誤的解決方法
今天在配置iis asp運(yùn)行環(huán)境的時(shí)候提示Microsoft JET Database Engine(0x80004005)未指定錯(cuò)誤,不過最后通過如下方法解決了,這里腳本之家小編特為大家分享下,方便需要的朋友2014-06-06
用ASP應(yīng)用程序?qū)崿F(xiàn)自己的UrlDeCode
URL編碼是指為了將信息通過URL進(jìn)行傳輸,所以必須將某些含有特殊意義的字符進(jìn)行替換的一種編碼方式,在asp中我們都知道有一個(gè)Server.URLEncode的函數(shù)可以完成這個(gè)功能。2006-08-08
利用Split函數(shù)進(jìn)行多關(guān)鍵字檢索
利用Split函數(shù)進(jìn)行多關(guān)鍵字檢索...2006-07-07
JavaScript在ASP頁面中實(shí)現(xiàn)掩碼文本框效果代碼
在網(wǎng)頁中輸日期、時(shí)間、IP地址等需要一定的格式限制,否則將會(huì)程序?qū)?huì)很難和程序溝通2012-08-08

