IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
C#
Search C# Group:

Advanced
-Collapse +Expand C# To/From
To/FromCODEGuides
-Collapse +Expand C# Study Test
PRESTWOODCERTIFIED
-Collapse +Expand C# Store
PRESTWOODSTORE

Prestwood eMagazine

June Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingC#WebForms Cod...   Print This     
  From the December 2015 Issue of Prestwood eMag
 
C# WebForms Coding Tasks:
FileUpload
 
Posted 12 years ago on 3/6/2008 and updated 11/3/2008
C# Code Snippet:
 A flashcard from our C# Flashcards Library
 A code snippet from our C# Code Snippets Page

KB100925

Demonstrates the code required to retreive and store a file uploaded via an ASP FileUpload object.  Assumes you have a web form with a FileUpload, a button, and a label control.  Also, this example stores the file uploaded into a folder named "Uploads" which is assumed to pre-exist.

Syntax Example:
string path=Server.MapPath(@"~\Uploads");
string SaveAsName=path+@"\"+FileUpload1.FileName;
try
{
FileUpload1.SaveAs(SaveAsName);
Label1.Text = FileUpload1.FileName + " uploaded.";
}
catch(Exception err)
{
Label1.Text = err.Message;
}

Here's the complete code snippet applied to a button's click event:

protected void Button1_Click(object sender, EventArgs e)
    {
        string path=Server.MapPath(@"~\Uploads");
        string SaveAsName=path+@"\"+FileUpload1.FileName;
       
        try
        {
            FileUpload1.SaveAs(SaveAsName);
            Label1.Text = FileUpload1.FileName + " uploaded.";
        }
        catch(Exception err)
        {
            Label1.Text = err.Message;
        }
    }

Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P145A1
Enter key:
Code Contributed By Bryan Valencia:

Bryan Valencia is the author of millions of lines of code and writes for PrestwoodBoards and Visual Studio Journey on-line magazines.

Visit Profile

 KB Article #100925 Counter
6821
Since 4/2/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2020 Prestwood IT Solutions.   [Security & Privacy]