How to Upload File in Asp.net

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

Default.aspx

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

Defalut.aspx.cs

protected void Submit1_ServerClick(object sender, EventArgs e)
{
if( ( File1.PostedFile != null ) && ( File1.PostedFile.ContentLength > 0 ) )
{
string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);

//Create Data Folder within the Location u Specified
string SaveLocation = “D:\UploadedFiles” + “\” + fn;

//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.”);
}
}

You can give the specified location to upload in SaveLocation string.

Download

Popularity: 2% [?]

Related posts:

  1. What Is ASP.Net?
  2. Absolute Position for Controls in Asp.net
  3. Simple Add,Edit,Retrieve and Delete in Asp.net
  4. Dot Net Interview Questions – Part 2
  5. How to open an application, file Using Shell execute in .net
You can leave a response, or trackback from your own site.

Leave a Reply

Designed by: Business Web Hosting | Thanks to Buy Icons, travel tips and Used Cars