c# - Taking an image and storing as string it from user using .net MVC 5
问题描述
I am a student developer in .Net MVC 5 and I want to insert image from new user to my database. I found a lot of method for byte[] data type but according to my researchs , there is no source for string data type.(Example; Entity Framework 5 Code first adding an image) . What can i do for string column of ImageUrl in my database? Could you help me at this issue ?
My Model :
public class usersModel{
public string userImageUrl{get;set;}
}
My Controller :
public class UsersController : Controller
{
AgmEntities db = new AgmEntities();
public ActionResult Create()
{
return View("Create", new usersModel());
}
[HttpPost]
public ActionResult Create(usersModel uModel,HttpPostedFileBase file)
{
if (!ModelState.IsValid)
{
return View("Create");
}
var user = new Users();
if(file !=null && file.ContentLength > 0)
{
string fileName = Path.GetFileName(file.FileName);
string imgPath = Path.Combine(Server.MapPath("~/User_Images/"), fileName);
file.SaveAs(imgPath);
}
user.userImageUrl = "~/User_Images/" + file.FileName;/*Error line*/
db.Users.Add(user);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
}
My cshtml :
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-group">
@Html.LabelFor(model => model.userImageUrl, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" name="ImageFile" />
@Html.ValidationMessageFor(model => model.userImageUrl, "", new { @class = "text-danger" })
</div>
</div>
}
解决方案
您必须在表单中添加新{ enctype="multipart/form-data"}
内容才能允许上传文件。
Html.BeginForm(
null, null, FormMethod.Post, new { enctype="multipart/form-data"})
将cshtml代码更改为:
@using (Html.BeginForm(
null, null, FormMethod.Post, new { enctype="multipart/form-data"}))
{
@Html.AntiForgeryToken()
<div class="form-group">
@Html.LabelFor(model => model.userImageUrl, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input type="file" name="ImageFile" />
@Html.ValidationMessageFor(model => model.userImageUrl, "", new { @class = "text-danger" })
</div>
</div>
}
if user.userImageUrl = "~/User_Images/" + file.FileName;/*Error line*/
并在这种情况下将这行代码移到里面file.FileName
可能为空。
if(file !=null && file.ContentLength > 0)
{
string fileName = Path.GetFileName(file.FileName);
string imgPath = Path.Combine(Server.MapPath("~/User_Images/"), fileName);
file.SaveAs(imgPath);
user.userImageUrl = "~/User_Images/" + file.FileName;/*Error line*/
}
parameter name
需要更改与文件类型相同的操作的另一注意事项name of input
:
<input type="file" name="ImageFile" /> to <input type="file" name="file" />
推荐阅读
- javascript - React Select 在多选时选择缩写下拉名称
- mysql - 检查Laravel的子表中是否存在2条具有不同user_id的不同记录
- laravel - 重置密码电子邮件未在 Laravel 8.53 中进行翻译
- java - 如何通过 JsonPath 配置将 JSON 反序列化为 POJO
- adsense - 可以在 pythonanywhere 免费计划上添加 adsense 吗?
- sql - PostgreSQL 对计数字段的分组查询作为 smth 和总计数字段的一部分
- c - 使用c中的动态分配从文件中读取多个矩阵
- docker - 将图像推送到 docker repo 后如何获取 sha 值
- angular - 如何在Angular的另一个订阅中使用订阅?
- python-3.x - Python 正则表达式 - 删除所有“。” 和特殊字符,小数点除外