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, I 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:
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:
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and their control panel, very easy to adapt to and their IT team is awesome!
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:
To test this out, load up the web site and:
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:
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, I 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:
- Right click the project in your solution explorer.
- Select New Folder.
- 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:
- Right click the project in your solution explorer.
- Select add new item...
- Select a web form.
- Name it 'Default.aspx'.
- Open Default.aspx up to design mode.
- Drag and drop a fileupload control onto the web form.
- Add a break line after the fileupload.
- Drag and drop a button underneath the fileupload.
- Change the ID property to 'btnUpload'.
- Change the Text property to 'Upload'.
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and their control panel, very easy to adapt to and their IT team is awesome!
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:
- Open up Default.aspx to design mode.
- Double click btnUpload to generate the click event method for it.
- Add the following using statements at the top of the Default.aspx.cs class:
using System.IO;
- 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.FileName);
//save the file to our local path
FileUpload1.SaveAs(fileName);
}
}
To test this out, load up the web site and:
- Click browse.
- Select a file.
- Click open.
- Click upload.
- 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> |
No comments:
Post a Comment