ASP.NET FILE UPLOAD EXAMPLE
Jose M. EstebanMarch 28, 2001
Level: Beginner / Intermediate
Download Source Code
ASP.NET provides an easy mechanism to upload files from a Web Browser to a Web Server. The class HtmlInputFile allows to handle programmatically the information submitted by the INPUT control of the type file, eg, . Remember that The Input Control of Type File Is Only Available In Web Browsers That Support HMTL 3.2 and Later Versions. This HTML Version is Supported by IE 3.02 and netscape 3.0 and their later versesions.
There are two objects that you will use to upload files. One is the INPUT control in the Web Browser and the other is the HtmlInputFile object in the server. The INPUT control will be embedded inside a form that will post the information encoded in a format suitable for a file transfer over the Internet. The INPUT control will also load the selected file into the Web Browser's memory, and will allow access to the HtmlInputFile object in the code that runs in the server through the INPUT control's ID. Ie, you use .
To upload a file, the form must post the data with the code format multiform / form-data instead of the default application / x-www-form-urlencoded that sends the form elements in a name / value pair base. This restriction arises from the types of formats that an object of the class HtmlInputFile is capable of processing and the characters that can traverse the Internet without confusing the network devices and software The following example shows how to set the encoding format of a form.:
Lofile.acess = "video / *"
The example that follows shows how to enable a web form for file uploads and how to answer back to the user with a message and information about the file that was just uploaded. The web form is contained in a panel (FileUploadForm) that is initially visible And the answer ispened in a panel (answermsg) That INITILLY DISABLE. ONCE The File Is Uploaded, The Web Form Panel Is Set To Visible.
THE Event That Handles The Submitted Form Runs At the Server As Indicated by The Submit Input TAG:
The Directory and File classes allow to create a folder to contain the uploaded files and to get the name of the uploaded file filtering the path. In order to use this classes we need to include the System.IO name space, we can do so by including the import directive. This entry allows the upload the name space into the web form application, for more information on this topic read the article How to Manipulate Files in ASP.NET:<%@ Import Namespace = "System.IO"%>
.
Demo this code
<% @ Import namespace = "system.io"%>
Sub UploadFile_Clicked (Sender As Object, E AS Eventargs)
DIM LSTRFILENAME AS STRING
DIM LSTRFILENAMEPATH AS STRING
DIM LSTRFILEFOLDER AS STRING
LstrfileFolder = "c: / tempuploadedfiles /"
'Gets the file name
lstrfilename = Lofile.PostedFile.FileName
Lstrfilename = file.getfilenamefromPath (lstrfilename)
'Creates the folder if it@ not exists
IF (not Directory.Directoryexists (lstrfilefolder).
Directory.createdDirectories (lstrfilefolder)
END IF
'Save Uploaded File To Server
Lstrfilenamepath = lstrfilefolder & lstrfilename
Lofile.postedFile.saveas (lstrfilenamepath)
'Sets the answer
Filename.text = lstrfilename
FILETYPE.TEXT = Lofile.PostedFile.ContentType
FileLength.Text = cstr (Lofile.PostedFile.ContentLength)
FILEUPLOADFORM.Visible = false
Answermsg.visible = true
End Sub
Script>
Select the file to upload to the server:
OnServerClick = "UploadFile_Clicked"
Runat = "server"
ID = SUBMIT1>
form>
Asp: panel>
Thanks your file
HAS Been Uplined
We removed
Bytes of the type
Asp: panel>
Body>
Html>