How can I get an ASP.net web form (v3.5) to post a file using a plain old <input type="file" />
?
I am not interested in using the ASP.net FileUpload server control.
Thanks for your suggestions.
How can I get an ASP.net web form (v3.5) to post a file using a plain old <input type="file" />
?
I am not interested in using the ASP.net FileUpload server control.
Thanks for your suggestions.
Here's a Code Project article with a downloadable project which purports to solve this. Disclaimer: I have not tested this code. http://www.codeproject.com/KB/aspnet/fileupload.aspx
use the HTML control with a runat server attribute
<input id="FileInput" runat="server" type="file" />
Then in asp.net Codebehind
FileInput.PostedFile.SaveAs("DestinationPath");
There are also some 3'rd party options that will show progress if you intrested
You'll have to set the enctype
attribute of the form to "multipart/form-data",
then you can access the uploaded file using the HttpRequest.Files collection.
The Request.Files collection contains any files uploaded with your form, regardless of whether they came from a FileUpload control or a manually written <input type="file">
.
So you can just write a plain old file input tag in the middle of your WebForm, and then read the file uploaded from the Request.Files collection.
In your 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>
In code behind :
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)));
}
}