上传头像mysql_上传头像 - MVC5+EF6+MySql企业网盘实战 - 爱整理
寫在前面
最近又開始忙了,工期緊比較趕,另外明天又要去駕校,只能一個(gè)功能一個(gè)功能的添加了,也許每次完成的功能確實(shí)不算什么,等將功能都實(shí)現(xiàn)了,然后在找一個(gè)好點(diǎn)的ui對(duì)前端重構(gòu)一下。
示例
這里采用最簡(jiǎn)單的form中上傳文件的方式。當(dāng)然也可以使用插件什么的。@using (Html.BeginForm("Register", "UserInfo", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
UserInfo
@Html.ValidationSummary(true)
@Html.LabelFor(model => model.Header, new { @class = "control-label col-md-2" })
驗(yàn)證碼
}
@Html.ActionLink("Back to List", "Users")
controller[HttpPost]
public ActionResult Register(UserInfo userInfo)
{
int saveCount = 0;
if (ModelState.IsValid)
{
var files = Request.Files;
if (files.Count > 0)
{
var file = files[0];
string strFileSavePath = Request.MapPath("~/Content/Images");
string strFileExtention = Path.GetExtension(file.FileName);
if (!Directory.Exists(strFileSavePath))
{
Directory.CreateDirectory(strFileSavePath);
}
file.SaveAs(strFileSavePath + "/" + userInfo.DisplayName + strFileExtention);
userInfo.Header = "/Content/Images/" + userInfo.DisplayName + strFileExtention;
}
_userInfoServiceRepository.Add(userInfo);
saveCount = _userInfoServiceRepository.SaveChanges();
}
if (saveCount > 0)
{
return RedirectToAction("Users");
}
else
{
return View(userInfo);
}
}
結(jié)果
總結(jié)
使用原生的提交表單是最簡(jiǎn)單的方式,其實(shí)如果在做移動(dòng)端的話在手機(jī)瀏覽器不支持flash或者h(yuǎn)tml5,這中方式也是最合適的方式。
總結(jié)
以上是生活随笔為你收集整理的上传头像mysql_上传头像 - MVC5+EF6+MySql企业网盘实战 - 爱整理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 文件管理软件 云服务器,使用KDE Pl
- 下一篇: Linux学习之VirtualBox安装