Asp.net| Upload Author: abigfrog (Millennium Elf) (★java★)
. Select the file to upload to the server locally
first of all thanks to the powerful features of ASP.net, the HTML control element he provides, makes it easy for us to select the file to upload through a dialog box. Of course, your browser must also be IE 3.02 or Netscape 3.0 version or a higher version of the browser. You can complete the work of selecting a local file by using the following statement:
<input id = "Lofile" type = "file" runat = "server" >
II. In order to successfully complete the file upload, the encoding in form must be "multipart/form-data" instead of the default encoding "Application/x-www-form-urlencoded" from. The specific code is as follows:
<form method = "POST" enctype = "multipart/form-data" runat = "server" >
</form >
three. Obtain the uploaded file name and several operations on the disk
you must introduce a namespace (NAMESAPCE)--system.io in order to obtain the uploaded file name. This namespace defines a number of classes on file and disk operations, in which some of the features of the software are accomplished through some of the methods and properties of these classes.
(1). Get the name of the uploaded file
uses the GetFileName method in the path class, as follows:
lstrfilename = LoFile.PostedFile.FileName
' NOTE: LoFile.PostedFile.FileName returns the file name chosen through the file dialog box,
This contains the directory information for the file
lstrfilename = Path.getfilename (lstrfilename)
' Remove directory information, return file name
(2). Determine whether the upload directory exists, do not exist on the establishment of
Create a directory to use the CreateDirectory method in the Directory class to determine whether the directory exists exists methods to use in the Directory class. Specifically as follows:
If (not directory.exists (lstrfilefolder)) Then
directory.createdirectory (Lstrfilefolder)
End If
' NOTE: Lstrfilefolder is the directory name that the user fills in, or the default directory name
Four. Upload selected files to the server
in front of the work has been completed, you can upload files, upload files relatively simple, use the following 2 lines of statements can be completed upload work.
Lstrfilenamepath = lstrfilefolder & Lstrfilename
' gets uploaded directory and file name
loFile.PostedFile.SaveAs (Lstrfilenamepath)
' uploading files to server
Five. Obtain and display the properties of the uploaded file
Filename.text = Lstrfilename
' Get file name
Filetype.text = LoFile.PostedFile.ContentType
' Get file type
Filelength.text = cStr (loFile.PostedFile.ContentLength)
' Get file length
fileuploadform.visible = False
answermsg.visible = True
' Display upload file properties
above is the software in the preparation of some of the more important places.
You can judge the type of file uploaded by the user in the program:
-----------------------
' Get file type
Filetype.text = LoFile.PostedFile.ContentType
-----------------------
then decide whether to accept or reject.