This tutorial will demonstrate how to upload a file using the file upload control with ASP.NET 4.0 and C#.

Creating the Web Site

For this tutorial we will create a simple web site that will allow us to upload files to a specific folder within the web site's file structure. At this point, we have create a new ASP.NET Empty Web Site. The first thing we will need to do is setup a folder local to our web site to which we will save the uploaded files. To begin:

1. Right click the project in your solution explorer.
2. Select New Folder.
3. Name the folder 'Files'.

Next, we need to setup the web page that the user will upload files with. For this, we will be adding in a file upload control and a button. To do this:

1. Right click the project in your solution explorer.
2. Select add new item.
3. Select a web form.
4. Name it 'Default.aspx'.
5. Open Default.aspx up to design mode.
6. Drag and drop a fileupload control onto the web form.
7. Add a break line after the fileupload.
8. Drag and drop a button underneath the fileupload.
9. Change the ID property to 'btnUpload'.
10. Change the Text property to 'Upload'.

This form will allow the user to select a file and then click upload to actually upload it to the web site.


Uploading Files

Next, we need to add some C# code that will actually upload a file in the event that the upload button is clicked and a file has been selected. To do this:

1. Open up Default.aspx to design mode.
2. Double click btnUpload to generate the click event method for it.
3. Add the following using statements at the top of the Default.aspx.cs class:

using System.IO;

4. Add the following code to the btnUpload_Click event method:

protected void btnUpload_Click(object sender, EventArgs e)
{
       //check to make sure a file is selected
       if (FileUpload1.HasFile)
       {
            //create the path to save the file to
            string fileName = Path.Combine(Server.MapPath("~/Files"), FileUpload1
            //save the file to our local path
            FileUpload1.SaveAs(fileName);
       }
}


Testing

To test this out, load up the web site and:

1. Click browse.
2. Select a file.
3. Click open.
4. Click upload.
5. Close the web site.

What this has done is uploaded the file that you selected to the Files folder of our web site. To view the file that was uploaded, open up your solution explorer and click the refresh icon. The file should now be listed in your files folder.

The Default.aspx source looks like this:

<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <br />
        <asp:Button ID="btnUpload" runat="server" Text="Upload" 
            onclick="btnUpload_Click" />
    </div>
    </form>
</body>


What is so SPECIAL on ASPHostDirectory.com ASP.NET 4.0 Hosting?

We know that finding a cheap, reliable web host is not a simple task so we’ve put all the information you need in one place to help you make your decision. At ASPHostDirectory, we pride ourselves in our commitment to our customers and want to make sure they have all the details they need before making that big decision.

We will work tirelessly to provide a refreshing and friendly level of customer service. We believe in creativity, innovation, and a competitive spirit in all that we do. We are sound, honest company who feels that business is more than just the bottom line. We consider every business opportunity a chance to engage and interact with our customers and our community. Neither our clients nor our employees are a commodity. They are part of our family.

The followings are the top 10 reasons you should trust your online business and hosting needs to us:

- FREE domain for Life - ASPHostDirectory gives you your own free domain name for life with our Professional Hosting Plan and 3 free domains with any of Reseller Hosting Plan! There’s no need to panic about renewing your domain as ASPHostDirectory will automatically do this for you to ensure you never lose the all important identity of your site
- 99,9% Uptime Guarantee - ASPHostDirectory promises it’s customers 99.9% network uptime! We are so concerned about uptime that we set up our own company to monitor people’s uptime for them called ASPHostDirectory Uptime
- 24/7-based Support - We never fall asleep and we run a service that is opening 24/7 a year. Even everyone is on holiday during Easter or Christmast/New Year, we are always behind our desk serving our customers
- Customer Tailored Support - if you compare our hosting plans to others you will see that we are offering a much better deal in every aspect; performance, disk quotas, bandwidth allocation, databases, security, control panel features, e-mail services, real-time stats, and service
- Money Back Guarantee - ASPHostDirectory offers a ‘no questions asked’ money back guarantee with all our plans for any cancellations made within the first 30 days of ordering. Our cancellation policy is very simple - if you cancel your account within 30 days of first signing up we will provide you with a full refund
- Experts in ASP.NET 4.0 Hosting - Given the scale of our environment, we have recruited and developed some of the best talent in the hosting technology that you are using. Our team is strong because of the experience and talents of the individuals who make up ASPHostDirectory
- Daily Backup Service - We realise that your website is very important to your business and hence, we never ever forget to create a daily backup. Your database and website are backup every night into a permanent remote tape drive to ensure that they are always safe and secure. The backup is always ready and available anytime you need it
- Easy Site Administration - With our powerful control panel, you can always administer most of your site features easily without even needing to contact for our Support Team. Additionally, you can also install more than 100 FREE applications directly via our Control  Panel in 1 minute!

Happy Hosting!