SIGN UP MEMBER LOGIN:    
ARTICLE

File Uploading in ASP.NET 2.0

Posted by Raghavendra u Articles | ASP.NET Programming August 12, 2010
This article tells us to upload any file into our system by using FileUpload Control in ASP.NET 2.0.
Reader Level:

This article tells us to upload any file into our system by using FileUpload Control in ASP.NET 2.0.
  1. Add new webform to your project.
  2. Name it as FileUploading.aspx.
  3. Drag and drop FileUpload control from toolbox into webform.
  4. Drag and drop Button control from toolbox into webform. Put Button Text to "Upload".
  5. Double click on the Button Control it generates "Button1_Click" event in .cs file.
  6. And finally drag and drop Label control from toolbox into webform.
Create a folder in D drive with the name "Uploads"

In FileUploading.aspx.cs, write the following code under button double click "Button1_Click" event

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(@"D:\Uploads\" + FileUpload1.FileName);
            lblFileMsg.Text = "<b>File Uploaded Successfully.</b><br>";
            lblFileMsg.Text += "File Name :" + FileUpload1.FileName;
            lblFileMsg.Text += "<br>File Type :" + FileUpload1.PostedFile.ContentType;
            lblFileMsg.Text += "<br>File Size :" + FileUpload1.PostedFile.ContentLength;
        }
        else lblFileMsg.Text = "Failed to Upload Your File";
    }
    catch (Exception)
    { }
}

By default it uploads up to 4MB size of files. It doesn't allow 'exe' files.

To increase the limit sizes of the file by adding a tag in web.config file. Add this tag in between.

<System.Web>
  <httpRuntime executionTimeout="500" maxRequestLength="4096" />
</System.Web>

Here the executionTimeout in milliseconds and maxRequestLength.

In kb. So, increase the maxRequestLength to upload large files.

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor