Page 1 of 1

Uploading File To Server

#1 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Posted 11 July 2009 - 05:01 PM

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.
<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.
<asp:TableCell >
     <asp:Button ID="cmdUploadFile" value="Upload" runat="server" Text="Upload" onclick="cmdUploadFile_Click" />
</asp:TableCell>



cmdUploadFile_Click Event
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.

Is This A Good Question/Topic? 1
  • +

Replies To: Uploading File To Server

#2 bed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 13-April 11

Posted 13 April 2011 - 07:17 PM

Hello eclipsed4utoo,

I was wondering where to place the cmdUploadFile_Click Event code. I have tried putting it in the controller, a seperate aspx page in home but neither work. I am new to ASP.net.

Thanks,
bed
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Posted 14 April 2011 - 05:33 AM

You put it in the code-behind for the ASPX page.
Was This Post Helpful? 0
  • +
  • -

#4 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 446
  • View blog
  • Posts: 1,501
  • Joined: 28-April 09

Posted 14 April 2011 - 09:03 PM

Nice little tutorial.

You could also use the FileUpload control

<asp:FileUpload ID="FileUpload1" runat="server" />



The generated html is the same as above so it's really just a matter of preference.

<input type="file" name="ctl00$MainContent$FileUpload1" id="MainContent_FileUpload1" />



Another note about file uploads is that they will not operate correctly within an UpdatePanel unless you add them as a PostbackTrigger.

This post has been edited by Nakor: 14 April 2011 - 09:05 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1