Request.Files is Empty in a Web Forms Application
I was fixing some bugs in my application today and ran into a tricky bug. My application is based on Webforms but I use a lot of jQuery and other more modern technologies. In one place I have an upload form that is dynamically populated with jQuery templates. Since i create all my <input type”file”> with jQuery i can’t use ASP.NET’s built-in <asp:FileUpload>, i can’t even have then as “runat=server”.
Today when I made some changes I removed the last file upload that had runat=” server” on it and a couple of hours later I noticed that all uploads had stopped working. After some debugging it hit me that I should check if the “auto-generated” form that asp.net ads to each page had the enctype=” multipart/form-data” attribute still. And of course, that was gone.
Naturally, ASP.NET removed that attribute when I removed the last file upload that had “runat=server”. So if you do not want any file upload that has runat=server or <asp:fileupload> you have to add type manually to your form.
Page.Form.Enctype = "multipart/form-data";