Home » ASP.Net, Tech Corner

FileUpload Control

15 April 2009 No Comments Posted By:LG

How to Upload a file in an ASP.NET website…???

Pre-requisites

1)Drag & drop the “Fileupload” control (“FileUpload1”)into an aspx page(“latest15.aspx” ).You will get “Fileupload” control from visualstudio.Net standard toolbar.

2)Add a button to the page( “Button_Upload”) .

The fileupload control itself helps browsing our system,I am not going on explaining just check out the code below.It is the code file of the page.

“latest15.aspx.cs”
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class latest15 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button_Upload_Click(object sender, EventArgs e)
{
FileUpload1.SaveAs(Server.MapPath(Request.ApplicationPath)+”\\”+FileUpload1.FileName);
}
}

Notes:
1)Savesas” is used to save the file,and the parameter for this method is the location where we need to save the file.
2)Server.MapPath(Request.ApplicationPath)” will give our project location,where all our files are kept.This will be normal inside the visualstudio folder->websites .
3)FileUpload1.FileName” will get the name of the selected file.

You might be surprised at first that no new file came in your solution explorer,But my code works for sure,right click on the projectname press “Refresh Folder”.The uploaded file will be there.

Hope this thing helps,don’t hesitate to ask any doubts.

Related posts:

  1. Multiview and View controls in ASP.NET(3.5)
  2. Edit,Update,Cancel In a GridView
  3. Dropdownlist inside a Gridview
  4. How to Bind Images from Database in a GridView
  5. .NET Interview Questions & Answers

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>