Why you need to get currect httpcontext, just use page 1, look at this example:
//aspx <form id="form1" runat="server" enctype="multipart/form-data"> <input type="file" id="myFile" name="myFile" /> <asp:Button runat="server" ID="btnUpload" OnClick="btnUploadClick" Text="Upload" /> </form> //c# protected void btnUploadClick(object sender, EventArgs e) { HttpPostedFile file = Request.Files["myFile"]; if (file != null && file.ContentLength ) { string fname = Path.GetFileName(file.FileName); file.SaveAs(Server.MapPath(Path.Combine("~/App_Data/", fname))); } }
Sample code from Uploading files to ASP.net without using the FileUpload control server
Btw, you do not need to use button control on the server side. You can add the above code to the loading page, where you check if the current state is a postback.
Good luck
Mouhannad
source share