<Input id = "lofile" type = "file" runat = "server">
<Form method = "Post" enctype = "multipart/form-Data" runat = "server">
</Form>
1. Get the name of the uploaded file: (the key is the application of the postedfile class)
Use the getfilename method in the path class as follows:
Lstrfilename = lofile. postedfile. filename 'Note: lofile. postedfile. filename returns the file name selected in the file dialog box, This includes the directory information of the file. Lstrfilename = path. getfilename (lstrfilename) 'Remove the directory information and return the file name |
(2) Determine whether the upload directory exists. If it does not exist, create
To create a directory, you must use the createdirectory method in the Directory class to determine whether the directory exists and the exists method in the Directory class. The details are as follows:
If (not directory. exists (lstrfilefolder) then Directory. createdirectory (lstrfilefolder) End if 'Note: lstrfilefolder is the directory name you enter or the default directory name. |
<4>. Upload the selected file to the server.
After the previous work has been completed, you can upload the file. It is relatively simple to upload the file. You can use the following two statements to complete the upload.
Lstrfilenamepath = lstrfilefolder & lstrfilename 'Get the upload directory and file name Lofile. postedfile. saveas (lstrfilenamepath) 'Upload files to the server |
<5>. Obtain and display the attributes of the uploaded file.
Filename. Text = lstrfilename 'Get the file name Filetype. Text = lofile. postedfile. contenttype 'Obtain the file type Filelength. Text = CSTR (lofile. postedfile. contentlength) 'Get the file length Fileuploadform. Visible = false Answermsg. Visible = true 'Display uploaded file attributes |
<% @ Import namespace = "system. Io" %>
<HTML>
<Body>
<Script language = "VB" runat = "server">
Sub uploadfile_clicked (sender as object, e as eventargs)
Dim lstrfilename as string
Dim lstrfilenamepath as string
Dim lstrfilefolder as string
'If the upload directory is empty, use "C:/" as the default upload directory.
'Get the directory name uploaded to the server
If dir. value <> "then
Lstrfilefolder = dir. Value
Else
Lstrfilefolder = "C :/"
End if
'Get the file name
Lstrfilename = lofile. postedfile. filename
'Note: What is returned by lofile. postedfile. filename?
The file name selected in the file dialog box, which contains the directory information of the file.
Lstrfilename = path. getfilename (lstrfilename)
'Remove the directory information and return the file name
'Determine whether the upload directory exists. If it does not exist, create
If (not directory. exists (lstrfilefolder) then
Directory. createdirectory (lstrfilefolder)
End if
'Upload files to the server
Lstrfilenamepath = lstrfilefolder & lstrfilename
'Get the upload directory and file name
Lofile. postedfile. saveas (lstrfilenamepath)
'Obtain and display the attributes of the uploaded file
Filename. Text = lstrfilename
'Get the file name
Filetype. Text = lofile. postedfile. contenttype
'Obtain the file type
Filelength. Text = CSTR (lofile. postedfile. contentlength)
'Get the file length
Fileuploadform. Visible = false
Answermsg. Visible = true
'Display uploaded file attributes
End sub
</SCRIPT>
<Asp: Panel id = "fileuploadform" visible = "true" runat = "server">
<Form method = "Post" enctype = "multipart/form-Data" runat = "server">
<H1> welcome to ASP. NET page for file upload Select the name of the file uploaded to the server:
<Input id = "lofile" type = "file" runat = "server"> <br>
Enter the name of the Directory uploaded to the server:
<Input id = "dir" type = "text" runat = "server"> <br>
<Input type = "Submit" value = "Start upload" onserverclick ="
Uploadfile_clicked "runat =" server ">
<Br>
</Form>
</ASP: Panel>
<Asp: Panel id = "answermsg" visible = "false" runat = "server">
Thank you for using the ASP. NET page for file upload. <br>
Uploaded successfully <asp: Label id = "FILENAME" runat = "server"/> <br>
File Size <asp: Label id = "filelength" runat = "server"/> bytes <br> file type <asp: label id = "filetype" runat = "server"/> <br>
</ASP: Panel>
</Body>
</Html>
Trackback: http://tb.donews.net/TrackBack.aspx? Postid = 240207