Reputation: 16354
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.
Upvotes: 2
Views: 3160
Reputation: 5989
Whenever we have a file to be uploaded in html form or whenever we are using tag in the form we have to notify the browser that the request contains binary data. Hence it achieve this you have to add a enctype attribute to the tag.
enctype="multipart/form-data" should be added to form.
it denotes that no characters are encoded before sent. i.e. it ensure that no character is encoded before sending the data to the server.
Upvotes: 1
Reputation: 819
The browser may be the cause. If you are using IE the files will be in Request.Files
, but in Chrome and FF the files are in Request.QueryString["qqfile"]
Here's an example with code
Upvotes: 0
Reputation: 17724
You are using the html form control instead of the asp.net form server control.
You will need to set the enctype
<form id="importManagerForm" enctype="multipart/form-data"
action="../ImportManager.ashx" method="POST">
Only then will you be able to receive files
Upvotes: 1
Reputation: 31202
Looks like you're missing the enctype="multipart/form-data"
attribute on your form.
Upvotes: 3