c# - 上传的 HttpPostedFile 为空

标签 c# asp.net asp.net-mvc

在 View 上:

<% =Html.BeginForm("About", "Home", FormMethod.Post, new {enctype="multipart/form-data "})%>
  <input type="file" name="postedFile" />
  <input type="submit" name="upload" value="Upload" />
<% Html.EndForm(); %>

在Controller中,有这样的东西:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult About(HttpPostedFile postedFile)
{
    //but postedFile is null 
    View();
}

postedFile 在 About() 中为空。如何上传文件?

最佳答案

使用 HttpPostedFileBase(不是 HttpPostedFile),并按照表单中的名称准确命名参数。例如。如果你有

<input type="file" id="file1" name="file1" />

你必须有方法头:

public ActionResult About(HttpPostedFileBase file1)

关于c# - 上传的 HttpPostedFile 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/544182/

相关文章:

c# - MSTest 如何确定运行测试方法的顺序?

c# - 如果用户使用 ASP.NET 在浏览器中直接输入 URL,如何重定向到登录页面

asp.net - 字段不应超过 4 个字符且字符应唯一

c# - UpdateModel() 有什么作用?

jquery - 如何在ListBox中加载大量数据? ASP.NET MVC 应用程序

asp.net-mvc - 使用 Azure 存储 blob 时 DownloadTo(stream) 与 DownloadTo(string) 之间的区别

c# - 简单的 JavaScript 问题,在 IE 中返回 NULL,但在 FireFox 中工作!

c# - 如何以可靠的方式管理外部资源?

c# - 使用 Ajax AutoCompleteExtender 请求 Web 服务时出现 500 错误

c# - N-Layer/N-Tier 错误处理设计