【问题标题】:HttpPostedFileBase error (file object was null)HttpPostedFileBase 错误(文件对象为空)
【发布时间】:2018-10-14 14:30:23
【问题描述】:

我的html文件index.cshtml是这样的

<html>
<head>
</head>
<body>
    <form method="post">
        <input type="file" name="myfile" id="myfile" />
        <input type="submit" value="Upload" />
    </form>
</body>
</html>

我的控制器是这样的

public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(HttpPostedFileBase myfile)
        {
            string currentdir = Directory.GetCurrentDirectory();

            myfile.SaveAs(currentdir + "\\" + myfile.FileName);

            return View();
        }
    }

当我发布文件时发生了错误。它告诉 myfile 对象为空。请帮助解决这个问题。太感谢了!

【问题讨论】:

  • 在表单标签中使用 enctype="multipart/form-data"

标签: c# asp.net asp.net-mvc model-view-controller


【解决方案1】:

在表单标签中添加 enctype 属性,如下所示,

<form method="post" enctype="multipart/form-data">

【讨论】:

    【解决方案2】:
    <html>
    <head>
    </head>
    <body>
        <form method="post" enctype="multipart/form-data">
            <input type="file" name="myfile" id="myfile" />
            <input type="submit" value="Upload" />
        </form>
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多