views:

519

answers:

1

Hello,

I have a web control with some custom javascript. The javascript creates new file upload controls on the client dynamically using code similar to:

var newFileUpload = document.createElement('input');
newFileUpload.type = 'file';
container.appendChild(newFileUpload); // where container is a div

This exists in an ASP.NET form with encType set to multipart/form-data. I will have 1 - n controls on the page (well, limited to a reasonable number, of course).

I now would like to capture the uploaded files in my ASP.NET application. Because of the approach taken above I know that I cannot capture them as I would from a FileUpload control (which unfortunately I cannot use). Is there another way to capture the uploaded files?

I have looked through a number of areas, including:

  • Request.Files
  • Request.Form
  • Request.Form.Keys
  • Request.InputStream

But I have not been able to find the content. I believe the client is submitting this data correctly but have been unable to determine what the server does with the raw request information (if this is even exposed).

Does anyone have any suggestions on areas I might further explore?

Thanks

+2  A: 

You should add a unique name to your upload element to get it from Request.Form collection.

var newFileUpload = document.createElement('input');
newFileUpload.type = 'file';
//newFileUpload.id = 'file01';
newFileUpload.name = 'file01';
container.appendChild(newFileUpload);

EDIT : I tried id and name attibutes, the with name, you can get the content by

Request.Form["file01"]

Also if you should add the attribute below to your form element. This allows you to get the file content by Request.Files["file01"] :

enctype="multipart/form-data"
Canavar
Hmmm, I've tried that, and it is still not in the Request.Form collection. Would there be anything else I need to do?
Chris
with the name attribute should work
Marwan Aouida
Ahhh, name - now that I have not tried. Thanks heaps, I will give that a go! I already have the encType, so should be good to go. Now you have jogged my memory I seem to remember request uses name not ID? Thanks :)
Chris
Hi Canavar - what language are you using. I have tried just about everyway to add name (in IE7, for prosperity detailed here http://stackoverflow.com/questions/611932/ie-is-not-submitting-dynamically-added-form-elements-is-this-an-ie-bug) but I cannot get it in the server Request object.... I am using C# (ASP.NET)
Chris
Ahh, my problem - had the control in an UpdatePanel, thanks for the answer!
Chris