CSV文件上传错误

时间:2013-09-18 16:59:27

标签: c# asp.net-mvc

当我想在ASP.Net MVC中尝试上传文件时,我收到以下错误。

文件错误

  

在administratorPortal.dll

中发生了'System.NullReferenceException'类型的第一次机会异常

文件错误

  

线程''(0x21e4)已退出,代码为0(0x0)。   在administratorPortal.dll

中发生了'System.NullReferenceException'类型的第一次机会异常

在我看来

<form action="../../Controllers/patientAppointmentController.cs" method=post>
<input id="model" type="file" name="fileUpload" data-val="true" data-val-required="File is required" />
<input class="btn btn-primary" type="submit" value="Import" />
</form>  

在我的控制器中

public ActionResult CSVUpload(HttpPostedFileBase fileUpload)
    {
        try
        {
            Debug.Write(fileUpload.ContentLength);

            if (fileUpload.ContentLength < 0 || fileUpload == null)
            {
                Debug.Write("unable to detectFile");
            }
        }
        catch
        {
            Debug.Write("file error");
        }
        return View();
    }

有一些问题,我甚至无法将文件传递给控制器​​。我尝试过在互联网上找到的许多不同的方法,但它们都不适合我。

  • 我安装了CSVhelper

2 个答案:

答案 0 :(得分:4)

表单似乎指向错误的位置

<form action="/patientAppointment/CSVUpload" method="post" enctype="multipart/form-data">
  <input id="model" type="file" name="fileUpload" data-val="true" data-val-required="File is required" />
  <input class="btn btn-primary" type="submit" value="Import" />
</form>  

正如@Fals指出的那样,你还需要使用HttpPost属性来装饰方法,以表明它接收到一个表单。

[HttpPost]
public ActionResult CSVUpload(HttpPostedFileBase fileUpload)
{
    try
    {
        Debug.Write(fileUpload.ContentLength);

        if (fileUpload.ContentLength < 0 || fileUpload == null)
        {
            Debug.Write("unable to detectFile");
        }
    }
    catch
    {
        Debug.Write("file error");
    }
    return View();
}

答案 1 :(得分:1)

表单的操作不应该指向资源的地址而不是.cs文件吗?

相关问题