Arizona1911
Arizona1911

Reputation: 2201

Uploading Files in ASP.NET

My web form creates Input file controls dynamically using javascript:

var input = document.createElement("input");
input.setAttribute("type", "file");
div.appendChild(input);

How do I get the "PostedFile" from these controls on the server side?

Upvotes: 2

Views: 124

Answers (3)

devmake
devmake

Reputation: 5262

If you prefer FileUpload control, you can generate few of the FileUpload controls on server (either through asp source with runat attribute or through application code) and hide them with css.

<asp:FileUpload ID="FileUpload1" runat="server" runat="server" class="hidden"/>

And then you can enable them with javascript after similar action as you would add new elements.

Then you can use the Files property of the Request object which returns a reference to the HttpFileCollection class. HttpFileCollection class has an Item property through which you get individual HttpPostedFile from the file collection either by specifying the name or index. See Listing 6 for an example.

Upvotes: 1

Mark
Mark

Reputation: 11740

The Request.Files collection will contain any files that were posted.

Upvotes: 2

David
David

Reputation: 218798

If it posts to the form with everything else then the contents should be in the Request.Files collection.

Upvotes: 2

Related Questions