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:
- Right click the project in your solution explorer.
- Select New Folder.
- Name the folder 'Files'.
We stand behind
Server Intellect and their support team. They offer
dedicated servers , and they are now offering
cloud hosting!
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'.
This form will allow the user to select a file and then click upload to actually upload it to the web site.
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:
- 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); } }
|
Testing
To test this out, load up the web site and:
- Click browse.
- Select a file.
- Click open.
- Click upload.
- Close the web site.
Yes, it is possible to find a good
web host. Sometimes it takes a while to find one you are comfortable with. After trying several, we went with
Server Intellect and have been very happy thus far. They are by far the most professional, customer service friendly and technically knowledgeable host we've found so far.
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>
|