2

When I want to try the file upload in ASP.Net MVC, I am receiving the following error.

file error

A first chance exception of type 'System.NullReferenceException' occurred in administratorPortal.dll

file error

The thread '' (0x21e4) has exited with code 0 (0x0). A first chance exception of type 'System.NullReferenceException' occurred in administratorPortal.dll

in my view

<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>  

in my controller

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();
    }

there is some problem, i cant even get the file passed to the controller. i had tried many different method found on the internet, but none of them work for me.

  • i had install CSVhelper
1
  • You are missing enctype="multipart/form-data" on the form Commented Sep 18, 2013 at 17:12

2 Answers 2

4

The form appears to be pointing to the incorrect location

<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>  

as @Fals has pointed out, you also need to decorate the method with the HttpPost attribute to indicate it receives a form.

[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();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Don't forget about the HttpPost Attribute for the action, I can't see it above!
i still have the above error. A first chance exception of type 'System.NullReferenceException' occurred in administratorPortal.dll Although i had followed the enctype and [HttpPost]. Is there anything else that i may have missed out?
1

Shouldn't your form's action point to the address of the resource and not the .cs file?

2 Comments

Hello Irwin, what do you mean by the address of the resource?
Oh, as @Kami pointed out, your form's action seemed incorrect, notice he/she changed it to: 'action="/patientAppointment/CSVUpload" '?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.