Nigel Whitworth's Development Site

You are currently not logged in.

How do I use the File Upload Control?

The first step in using a file upload control is to put the control on the form, either using the designer or coding it using a text editor like notepad. The code for an upload using a text editor is :-

<asp:FileUpload ID="upFiles" runat="server" Width="100%" ToolTip="" />

You will need to add additional properties depending on how you want it to look and act.

PropertyDescription
ID Unique name of the control on the form.
AccessKey Key on the keyboard when pressed in conjuction with the ALT key will shift focus to this control.
BackColor Colour of background where the chosen file is displayed.
BorderColorColour of border of control.
BorderStyleStyle of border of the control can be Dotted, Dashed, None, etc.
BorderWidthWidth of the border of the control.
CssClassElement name in the CSS file that corresponds with the presentation of the control.
EnabledWhether the user can access and use the control or not.
EnableThemingIn brief, theming is equivalent to CSS files but the presentation is added to the control before the HTML is sent to the client.
EnableViewStateViewState is true automatically, this feature allows you to turn off the Viewstate property for this control.
Font-BoldIs the text in the file box bold?
Font-ItalicIs the text in the file box
Font-NamesFonts to be used in the control, if the first doesn't exist use the second.
Font-OverlineIs the text in the file box to have an overline
Font-SizeSize of the font in the file box
Font-StrikeoutIs the text in the file box to be struck out.
Font-UnderlineIs the text in the file box to be underlined.
ForeColorColour of the font in the file box.
HeightHeight of the control.
runatAlways server
SkinIDItem in the relevent SKIN file that dictates what the control is to look like.
TabIndexAt what point when presssing the tab key will this control get focus.
ToolTipWhen you hover over the control, a small tool tip will appear describing the control.
VisibleWhether the control is visible or invisible.
WidthWidth of control either in Pixels or Percentage.

Once the user has selected a file to upload and submitted the control, you want to process it. The processing is done on the submit control's event handler. Below is example code.

     ' Check that the user has uploaded a file.
     If Not upFiles.PostedFile Is Nothing And upFiles.PostedFile.ContentLength > 0 Then
          ' Work out where to put the file. Server.MapPath("DATA") will return the location on your hard disk of a folder called DATA in your ASP.NET directory.
          strSaveLocation = Server.MapPath("Data") & "\" & CStr(lngCallID) & ".jpg"
          ' Be safe and wrap the code in error handling.
          Try
               ' Store the uploaded file to where you want it to be stored.
               upFiles.PostedFile.SaveAs(strSaveLocation)
          Catch Exc As Exception
               Response.Write("Error: " & Exc.Message)
          End Try
     Else
     ' If no file was uploaded, do whatever has to be done.
     End If

The equivalent in C# is :-

     // Check that the user has uploaded a file.
     if (upFiles.PostedFile != null && upFiles.PostedFile.ContentLength > 0)
     {
          /* Work out where to put the file. Server.MapPath("DATA") will return the location on your hard disk of a folder called DATA in your ASP.NET directory. */
          strSaveLocation = Server.MapPath("Data") + "\\" + (lngCallID).ToString + ".jpg";
          // Be safe and wrap the code in error handling.
          try
          {
               // Store the uploaded file to where you want it to be stored.
               upFiles.PostedFile.SaveAs(strSaveLocation);
          }
          catch (Exception ex)
          {
               Response.Write("Error: " + ex.Message);
          }
     }
     else
     {
     // If no file was uploaded, do whatever has to be done.
     }

File Upload has been completed.


If you intend to vote or make a comment, please enter the security code.

Make a comment

*Comments are the views of individuals, they may or may not be correct.
All comments are reviewed and accepted or rejected.
If you give an email address, you will be sent an email when someone makes a comment on this page.*

Name :
Email :
Comments :


Only name and Code is compulsory.
 
 
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)