Upload the File in ASP.NET

Labels:

In this example we browse a file from our pc and upload that file to some other loaction in our pc.

Default.aspx

\\Place a file type textbox and a button




<input type=file id=File1 name=File1 runat="server">
<input type="submit" id="Submit1" value="Upload" runat="server" name="Submit1">

Defalut.aspx.cs

if( ( File1.PostedFile != null ) && ( File1.PostedFile.ContentLength > 0 ) )
{
string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
string SaveLocation = Server.MapPath("Data") + "\\" + fn;\\Create Data Folder within the Location u Specified
\\File will be Uploaded to the Specified Location
try
{
File1.PostedFile.SaveAs(SaveLocation);
Response.Write("The file has been uploaded.");
}
catch ( Exception ex )
{
Response.Write("Error: " + ex.Message);
}
}
else
{
Response.Write("Please select a file to upload.");
}

0 comments:

Post a Comment