请求.mvc中按名称列出的文件

本文关键字:文件 mvc 请求 | 更新日期: 2023-09-27 18:29:25

我传递了一个文件名,然后我想请求.files,但它一直返回null。这是代码:

 @using (Html.BeginForm("AddTechNote", "Ticket", FormMethod.Post))
 <div class="col-md-10">
       <input type="file" id="fileToUpload" name="fileToUpload" />
       <span class="field-validation-error" id="spanfile"></span>
 </div>

在控制器中:

public ActionResult AddTechNote(TicketView ticketReturn, string Note, bool PublicNote, string fileToUpload, string CopyIntoEmail)
{
        HttpPostedFileBase file = Request.Files[fileToUpload];
        string _fileName = null;
        if (file != null && file.ContentLength > 0)
        {
               _fileName = new FileController().UploadFile(file, "Tickets", ticketReturn.TicketNumber.ToString());
        }

视图不是强类型的(至少不属于此模型)。输入字段在表单中。

请求.mvc中按名称列出的文件

不需要string fileToUpload,而是需要HttpPostedFileBase fileToUpload,并在后续代码中使用fileToUpload,如下所示。

public ActionResult AddTechNote(TicketView ticketReturn, string Note, bool PublicNote, HttpPostedFileBase fileToUpload, string CopyIntoEmail)
{
        string _fileName = null;
        if (fileToUpload != null && fileToUpload.ContentLength > 0)
        {
               _fileName = new FileController().UploadFile(fileToUpload, "Tickets", ticketReturn.TicketNumber.ToString());
        }
  ...........
}

还要确保为BeginForm设置了multipart/form-data,例如-

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))