I have an application page in LAYOUTS in the 14 hive (SP 2010) within which I am dynamically appending HTML <input type="file"/> controls onto the page. Now when I try to post these files to the server to save them to a document library the HttpContext.Current.Request.Files collection is always empty.

Similar to this question: Sharepoint webpart upload file whitout[sic] using “runat server”

I can not use the <asp:FileUpload/> control with AllowMultiple set to true as there are many metadata input controls that are also being dynamically added to the page. I can not use a static amount of HTML file upload controls either; I have to add additional HTML file input controls client side.

I have found, from both the HttpRequest.Files Property MSDN page as well as various other places (like Request.Files is Empty in a Web Forms Application), that the HTML <form> elements encType value must be set to "multipart/form-data", which I am doing via:

protected void Page_Init(object sender, EventArgs e)
{
    Page.Form.Enctype = "multipart/form-data";
}

When debugging the page on post-back, the boundary=... section of the Request.ContentType value is appropriately being appended to the "multipart/form-data" value, but the HttpContext.Current.Request.Files collection is empty. (A nice explanation of the purpose of the boundary data is available in Scott Hanselman's blog post A Back To Basics Case Study: Implementing HTTP File Upload with ASP.NET MVC including Tests and Mocks). You can see the behavior I'm talking about in the debugger here:

HttpContext.Current.Request.Files Debug Info

Is this something to do with how requests are being handled for the Layouts directory? Or am I just missing something? Any help or pointers are greatly appreciated.

You don't need to use Page.Form.Enctype = "multipart/form-data";.

When you are working with Application pages you have to use Page.Request.Files which gives you a HttpFileCollection object. This contains all files from all fileupload controls that has the runat="server" attribute on the page.

  • Both collections are empty: Page.Request.Files and HttpContext.Current.Request.Files. The issue is that the input controls are dynamically added to the page client side so there is no runat="server" attribute. Normally in ASP.NET, the <input type="file"/> controls post their data to the server in a multipart request and can be accessed via the HttpRequest.Files collection, but this doesn't seem to be working. The documentation for HttpRequest.Files that I linked to above says: "The file collection is populated only when the HTTP request Content-Type value is 'multipart/form-data'." – RJ Cuthbertson Jul 2 '14 at 15:38
  • @RJCuthbertson I have multiple working solutions where multipart/form-data is not anywhere on the page. If the input controls are dynamically added, by javascript I presume, what prevents you from having a javascript run afterwards which adds the runat="server" attribute? – user2536 Jul 2 '14 at 16:57
  • Adding runat="server" client side doesn't do anything. It's just for the page parser, so that attribute needs to exist server side to actually have an effect. – RJ Cuthbertson Jul 2 '14 at 18:03

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.