Search code examples
c#asp.netashx

Cannot read file uploaded in c# handler


I'm trying to upload a file to a handler in c# but it seems as though the file is not getting uploaded. Calling Request.Files["fileNameHere"] returns null

My html code:

<form id="importManagerForm" action="../ImportManager.ashx" method="POST">
   <input name="selectedFile" id="selectedFile" type="file" /> 
   <input type="submit" value="submit"/>
</form>

And the code in the ashx handler is:

public void ProcessRequest(HttpContext context)
{
  var importFile = context.Request.Files["selectedFile"]; //This part returns null
  var fileName = new Guid().ToString() + ".csv";
  importFile.SaveAs(fileName);
}

Any idea what's the problem?

UPDATE:

A quick debug on context.Request.Files showed me a file count of 0.


Solution

  • Looks like you're missing the enctype="multipart/form-data" attribute on your form.