我正在尝试上传文件,但是在保存文件时会引发操作错误,即未设置对象引用,并且值也为NULL。
与传递和保存该类相同,但仍会引发错误。为什么它为空?它绑定到相同的视图模型。
@model VAILCertificates.UI.ViewModels.AddInspectionReportViewModel
@using (Html.BeginForm("Create", "InspectionReport", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-group">
@Html.LabelFor(model => model.File, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-5">
@Html.TextBoxFor(model => model.File, new { @class = "form-control", type = "file" })
@Html.ValidationMessageFor(model => model.File, "", new { @class = "text-danger" })
</div>
</div>
<button class="btn btn-success btn-outline btn-block" type="submit"><i class="fa fa-save" style="font-size:medium"></i> Add</button>
}
操作:
public ActionResult Create(AddInspectionReportViewModel model, HttpPostedFileBase FileName)
{
string FilePath = Server.MapPath("~/Downloads/Certificates/");
model.File.SaveAs(FilePath + Path.GetFileName(model.File.FileName));
model.InspectionReport.FileName = Path.GetFileName(model.File.FileName);
InspectionReportDAL.AddInspectionReport(model.InspectionReport);
return RedirectToAction("Index");
}
Viewmodel:
public class AddInspectionReportViewModel
{
public HttpPostedFile File { get; set; }
public InspectionReport InspectionReport { get; set; }
}
班级:
public class InspectionReport
{
//General Details
public int InspectionReportID { get; set; }
public string FileName { get; set; }
}
更新:内部错误是
The parameter conversion from type 'System.Web.HttpPostedFileWrapper' to type 'System.Web.HttpPostedFile' failed because no type converter can convert between these types.
答案 0 :(得分:1)
HttpPostedFileBase FileName
model.File
该方法期望的参数名称是FileName。
编辑此@Html.TextBoxFor(model => model.File, new { @class = "form-control", type = "file" })
到
@Html.TextBoxFor(model => model.File.FileName , new { @class = "form-control", type = "file" })
根据评论,我将尽可能执行以下操作。
如果我是你,我将删除@Html.TextBoxFor(model => model.File, new { @class = "form-control", type = "file" })
并替换为:
<input type="file" name="FileName" id="fileUpload or whatever" accept="//Whatever file you want to accept">
在当前状态下,您永远不会传递任何参数到HttpPostedFileBase
参数,仅传递模型,因此它始终为null。
答案 1 :(得分:1)
将viewModel更改为:
public class AddInspectionReportViewModel
{
[DataType(DataType.Upload)]
public HttpPostedFileBase File { get; set; }
public InspectionReport InspectionReport { get; set; }
}
将视图更改为:
@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-group">
@Html.LabelFor(model => model.File, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-5">
@Html.TextBoxFor(m => m.File, new { type = "file" })
@Html.ValidationMessageFor(model => model.File, "", new { @class = "text-danger" })
</div>
</div>
<button class="btn btn-success btn-outline btn-block" type="submit"><i class="fa fa-save" style="font-size:medium"></i> Add</button>
}
答案 2 :(得分:0)
将HttpPostedFile更改为HttpPostedFileBase可行。
public class AddInspectionReportViewModel
{
public HttpPostedFileBase File { get; set; }
public InspectionReport InspectionReport { get; set; }
}