Mvc razorview上传3个文件并将文件名保存到数据库中的3个不同列
我有3个表单输入3个文件,我将不得不将它们保存到数据库(sqlserver)中的不同列中。我无法弄清楚如何将每个文件名添加到单独的列。请帮忙。我试图在我的控制器中实现注释掉的代码,但我不确定语法是什么。我的代码如下:Mvc razorview上传3个文件并将文件名保存到数据库中的3个不同列
查看:
@Html.TextBoxFor(model => model.SerialAttachment, new { type = "file", name = "file1", id="file1" })
@Html.TextBoxFor(model => model.CountryAttachment, new { type = "file", name = "file2", id = "file2" })
@Html.TextBoxFor(model => model.OtherAttachment, new { type = "file", name = "file3", id = "file3" })
控制器:
`public ActionResult Create([Bind(Include = "Id,Serial,PinNumbers,SerialAttachment,CountryAttachment,OtherAttachment")] ModelName modelInstance`) {
if (Request.Files.Count > 0)
{
for (int i = 0; i < Request.Files.Count; i++)
{
var fileUp = Request.Files[i];
if (fileUp != null && fileUp.ContentLength > 0)
{
var fname = Path.GetFileName(fileUp.FileName);
var path = Path.Combine(Server.MapPath(fname));
fileUp.SaveAs(path);
// modelInstance.SerialAttachment = fname;
// modelInstance.CountryAttachment = fname;
// modelInstance.OtherAttachment = fname;
db.model.Add(modelInstance);
db.SaveChanges();
}
}
return RedirectToAction("Index");
}
型号:
public partial class ModelName
{
public int? Serial { get; set; }
public int? PinNumbers { get; set; }
[StringLength(250)]
public string SerialAttachment { get; set; }
[StringLength(250)]
public string CountryAttachment { get; set; }
[StringLength(250)]
public string OtherAttachment { get; set; }
}
您可以将文件输入不绑定到string
财产。首先创建一个包含在视图中所需的属性视图模型(总是使用view model编辑时的数据))
public class ModelNameVM
{
public int? PinNumbers { get; set; }
public HttpPostedFileBase SerialAttachment { get; set; }
public HttpPostedFileBase CountryAttachment { get; set; }
public HttpPostedFileBase OtherAttachment { get; set; }
}
,并添加显示和验证属性作为适当
你的看法届时将
@model ModelNameVM
....
@using (Html.BeginForm())
{
....
@Html.TextBoxFor(m => m.SerialAttachment, new { type = "file" })
@Html.TextBoxFor(m => m.CountryAttachment , new { type = "file" })
....
请注意,使用new { name = "..." }
什么也不做,并且不需要覆盖由HtmlHelper
方法创建的默认id
属性。
在POST方法中的代码后会有
public ActionResult Create(ModelNameVM model)
{
if (!ModelState.IsValid)
{
return View(model);
}
// Initialize a new instance of your data model and map values from the view model
ModelName data = new ModelName
{
PinNumbers = model.PinNumbers
};
if (model.SerialAttachment != null && model.SerialAttachment.ContentLength > 0)
{
string fileName = Path.GetFileName(model.SerialAttachment.FileName);
.....
model.SerialAttachment.SaveAs(path);
data.SerialAttachment = path
}
.... // repeat for CountryAttachment and OtherAttachment
db.model.Add(data);
db.SaveChanges();
return RedirectToAction("Index");
}
注意事项使用视图模型,当你不需要[Bind]
属性(您已经受到保护,免受过张贴攻击。
此外,你不是映射到你的应用程序中的文件夹上传文件,你需要类似的东西
var path = Path.Combine(Server.MapPath("~/Images"), fileName);
谢谢,我想我明白了。我正在尝试它,并会很快回复您。 – user7221204
工作完美!谢谢你这么清楚的解释! – user7221204
如果我要将一个自动编号字段(即“Id”)附加到文件名并将其分配给路径,我将如何在插入之前获取id值。如下所示:var path = Path.Combine(Networkpath,data.Id + fileName);我得到的ID为0,因为我想使用尚未被插入数据库的ID。 – user7221204
开始删除'new {name =“file1”}'等,这绝对不是NG。并且你的POST方法的签名应该有一个参数,它是你的模型,以便这3个'HttpPostedFileBase'属性绑定在你的模型中 –
但是你注释掉代码表明这3个属性是'string',在这种情况下你不能绑定一个文件输入到一个“字符串”(你需要绑定到属性是'HttpPostedFileBase') –
我只想将每个文件名保存到不同的列名。我已经完成了1个文件的工作,但我不确定如何区分不同的文件 – user7221204