Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在mvc中按名称请求.Files_C#_Asp.net Mvc_Razor - Fatal编程技术网

C# 在mvc中按名称请求.Files

C# 在mvc中按名称请求.Files,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我传入了一个文件名,然后我想请求.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

我传入了一个文件名,然后我想请求.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>

视图不是强类型的(至少不是此模型)。输入字段位于表单内部

您需要使用
HttpPostedFileBase fileToUpload
而不是
string 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" }))

您必须将
字符串fileToUpload
设置为
HttpPostedFileBase fileToUpload
,因为fileToUpload是
输入类型='file'
。然后使用
fileToUpload
获取文件内容。使用
fileToUpload.FileName
获取文件名。哇,如果是这样的话-对不起,我是个白痴。让我test@ramiramilu我现在在请求时收到一个错误。文件行说重载方法有无效参数。@Ramilu,不,这是不正确的。Request.Files使用字符串作为键,而不是postedfilebase。@Rahul是的,我在最初的评论中已经提到了使用FileName,如果我们已经获得了HttpPostedFileBase,那么为什么还要使用
Request.Files[]
。我们需要做的就是使用该对象并使用
.FileName
获取文件名。我已经这样做了,但文件仍然返回空。我正要更改原始问题以反映这一点。您是否使用了与输入type='file'名称相同的参数名称?同时向我们展示您的
Html.BeginForm()
代码。它应该是
multipart
form-like-
@使用(Html.BeginForm(“Index”,“Home”,FormMethod.Post,new{enctype=“multipart/form data”}))
这不是我在上面的代码视图中显示的吗?Ramiramiu,我刚刚在我的操作系统中添加了这一点。您可以尝试将multipart-form-part部分也放在代码中吗。
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))