School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,015 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,072 people online right now. Registration is fast and FREE... Join Now!




Uploading File To Server

 
Reply to this topicStart new topic

> Uploading File To Server

eclipsed4utoo
Group Icon



post 11 Jul, 2009 - 04:01 PM
Post #1


If you've ever wanted to add an upload feature to your website that allows visitors to upload files to a the server, then this is the tutorial for you.

It's actually fairly simple.

This code will open an OpenFileDialog box to allow the user to select a file.
html

<asp:TableCell>
Select File:
<input id="uplTheFile" type="file" runat="server" style="width: 476px; height: 26px" />
</asp:TableCell>


This code will add a button to the form to upload the file.
html

<asp:TableCell >
<asp:Button ID="cmdUploadFile" value="Upload" runat="server" Text="Upload" OnClick="cmdUploadFile_Click" />
</asp:TableCell>


cmdUploadFile_Click Event
c#

protected void cmdUploadFile_Click(object sender, EventArgs e)
{
string strFileNameOnServer = uplTheFile.PostedFile.FileName;
string appDataPath = HttpContext.Current.Server.MapPath("~/App_Data");

if (string.IsNullOrEmpty(strFileNameOnServer))
{
lblInformation.Text = "Error - a file name must be specified.";
return;
}

if (uplTheFile.PostedFile != null)
{
try
{
uplTheFile.PostedFile.SaveAs(appDataPath + "\\" + strFileNameOnServer);
}
catch (Exception ex)
{
lblInformation.Text = "Error saving <b>" + strFileNameOnServer + "</b><br>. " + ex.Message;
}
}
}


And that's it.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 07:32AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month