Thursday, October 7, 2010

File Upload Server Control

This control is used to upload a file and save file to a specific location using some properties.
A typical File Upload Control is constructed in the following manner:

<asp:FileUpload ID="FileUpload1" runat="server" />

For an Example of using FileUpload control and to save on local computer.



I am using extra button control and one label control.

.aspx

<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Upload" onclick="Button1_Click" />
<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>

.cs

protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
FileUpload1.SaveAs("C:\\Uploads\\"+FileUpload1.FileName);
Label1.Text = "File name: " +
FileUpload1.FileName + "<br/>" +
FileUpload1.PostedFile.ContentLength + " kb<br/>" +
"Content type: " +
FileUpload1.PostedFile.ContentType;
}
catch(Exception ex)
{
Label1.Text = ex.Message.ToString();
}
}
else
{
Label1.Text = "You have not added a file";
}
}

You can check whether your file uploaded to your local folder or disk.

If you see your file that’s it you are done with this control.

Thanks
Emmaneale Mendu
Web Developer

2 comments:

  1. after that how to store in database like columns as filename,filetype etc

    ReplyDelete