C# 上载的文件为空

C# 上载的文件为空,c#,html,asp.net-mvc,file-upload,asp.net-mvc-4,C#,Html,Asp.net Mvc,File Upload,Asp.net Mvc 4,在ASP.NET MVC4中,文件上载视图总是向控制器发送null。 我不知道为什么,我不知道如何修复它,搜索已经证明是徒劳的 控制器方法: [HttpPost] [ValidateInput(false)] public ActionResult uploadCustomImage(int id, HttpPostedFileBase file) {} 视图: @使用(Html.BeginForm(“uploadCustomImage”,“W”,new{id=ViewBag.id},Form

在ASP.NET MVC4中,文件上载视图总是向控制器发送null。 我不知道为什么,我不知道如何修复它,搜索已经证明是徒劳的

控制器方法:

[HttpPost]
[ValidateInput(false)]
public ActionResult uploadCustomImage(int id, HttpPostedFileBase file)
{}
视图:

@使用(Html.BeginForm(“uploadCustomImage”,“W”,new{id=ViewBag.id},FormMethod.Post,new{enctype=“multipart/form data”,name=“uploadingimage”}))
{ 
@Html.SubmitButton()
}
它可以很好地进入控制器,因此路由都很好。但文件总是空的。我尝试了几种不同的方法:重命名输入/对象,不使用文件参数,并调用以下命令: HttpPostedFileBase file=Request.Files[“file”]; (结果也是空的)

我尝试将formcollection包含为参数(有和没有不必要的表单元素)。这个文件仍然是空的

当然,在按下submit=p之前,我选择了一个文件,我尝试了多个文件;具有非常基本的文件名(没有奇怪的unicode,甚至没有空格)和更奇怪的文件名。大文件,小文件


一切都是空的!我哪里出错了?

你的名字不一致。。。您需要在控制器中对名为“postedFile”的文件控件进行模型绑定

@using (Html.BeginForm("uploadCustomImage", "W", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
   <input name="fileToUpload" type="file" />
   <input type="submit" />
}

[HttpPost]
[ValidateInput(false)]
public ActionResult uploadCustomImage(int id, HttpPostedFileBase fileToUpload)
{}
@使用(Html.BeginForm(“uploadCustomImage”,“W”,FormMethod.Post,new{enctype=“multipart/formdata”}))
{
}
[HttpPost]
[验证输入(错误)]
public ActionResult uploadCustomImage(int-id,HttpPostedFileBase-fileToUpload)
{}
[编辑]

我刚刚在我正在进行的一个项目中实现了一个上传功能,它可以正常工作

@using (Html.BeginForm("Import", "MY_CONTROLLER", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file"  id="UploadedXlsFile" name="UploadedXlsFile"/>

    <input type="submit" id="Submit" name="Submit" value="Submit"/>

}


public ActionResult Import()
{
    if (Request.Files["UploadedXlsFile"].ContentLength > 0)
    {
       .............Do stuff ................
    }
}
@使用(Html.BeginForm(“导入”、“我的控制器”、FormMethod.Post、新的{enctype=“multipart/formdata”}))
{
}
公共操作结果导入()
{
if(Request.Files[“UploadedXlsFile”].ContentLength>0)
{
做事。。。。。。。。。。。。。。。。
}
}

前面的代码适合我。如果我在Action方法中添加一个断点,我可以看到文件不是空的。

啊,谢谢,这是因为我尝试调用了file以外的其他名称。它们都是“postedFile”和“file”(同时)并产生了相同的结果。在longshot上,它是一个大文件吗?在另一个longshot上,更改浏览器。这会改变什么吗?我尝试了一些文件,最小的是3.58kb,在IE9和Firefox中测试过,结果一致为null。你想让我更改操作的名称吗?好啊仍然为空。抱歉,我已编辑了我的答案。它可能是您用于文件属性的名称吗?请为文件输入尝试其他名称。
@using (Html.BeginForm("Import", "MY_CONTROLLER", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <input type="file"  id="UploadedXlsFile" name="UploadedXlsFile"/>

    <input type="submit" id="Submit" name="Submit" value="Submit"/>

}


public ActionResult Import()
{
    if (Request.Files["UploadedXlsFile"].ContentLength > 0)
    {
       .............Do stuff ................
    }
}