Some materials collected earlier --- How to establish programming ideas for uploading Components
Keywords:ASP
In the previous post, I mentioned the comparison of several Upload components.
Now we can build our own Upload components.
This Upload Component should have the following features:
1. It should be able to accept the values transmitted from various HTML form elements,
It is transmitted through text or select.
2. An upload path should be provided.
3. The size of the uploaded file should be limited.
4. It should be able to support simultaneous uploading of multiple files
5. Handle exception errors
6. Should be able to work stably
7. You should be able to work in IE and Netscape at the same time)
8. Ability to save files in the database
9. User Permissions should be restricted
The code and files are as follows (I will not explain them in detail)
1. Upload.htm
<HTML>
<Head> <title> upload </title> <Body>
<Form name = "frmupload" method = "Post" enctype = "multipart/form-Data" Action = "Upload. asp"> <Table>
<Tr> <TD> author </TD> <input type = "text" name = "txtauthor"> </TD> </tr>
<Tr> <TD> file </TD> <input type = "file" name = "txtfilename"> </TD> </tr>
<Tr> <TD colspan = "2" align = "right"> <input type = "Submit" value = "Upload"> </TD> </tr>
</Table>
</Form>
</Body>
</Html>
** Note: enctype = "multipart/form-Data" is used to allow the form to submit a file.
2. Upload. asp
<% @ Language = VBScript %>
<%
Option explicit
Response. Buffer = true
On Error resume next
If request. servervariables ("request_method") = "Post" then
Dim objupload
Dim lngmaxfilebytes
Dim struploadpath
Dim varresult
Lngmaxfilebytes = 10000
Struploadpath = "C:/inetpub/wwwroot/upload /"
Set objupload = server. Createobject ("pjuploadfile. clsupload ")
If err. Number <> 0 then
The response. Write component is not correctly installed. "
Else
Varresult = objupload. doupload (lngmaxfilebytes, struploadpath)
Set objupload = nothing
Dim I
For I = 0 to ubound (varresult, 1)
Response. Write varresult (I, 0) & ":" & varresult (I, 1) & "<br>"
Next
End if
End if
%>
Using VB6 to develop this ActiveX Control: (Note that some code may be incomplete due to my laziness,
But it is important to understand the programming idea of this component)
1. Reference the Active Server Pages Object Library.
2. The Code is as follows:
Option explicit
Private myscriptingcontext as scriptingcontext
Private myrequest as request
Private myresponse as request
Private const err_no_filename as long = vbobjecterror+ 100
Private const err_no_extension as long = vbobjecterror + 101
Private const err_empty_file as long = vbobjecterror + 102
Private const err_filesize_not_allowed as long = vbobjecterror + 103
Private const err_folder_does_not_exist as long = vbobjecterror + 104
Private const err_file_already_exists as long = vbobjecterror + 105
Public sub onstartpage (passedscriptingcontext as scriptingcontext)
Set myscriptingcontext = passedscriptingcontext
Set myrequest = myscriptingcontext. Request
Set myresponse = mysriptingcontext. Response
End sub
Private function getfilename (strfilepath) as string
Dim intpos as integer
Getfilename = strfilepath
For intpos = Len (strfilepath) to 1 step-1
If mid (strfilepath, intpos, 1) = "/" or mid (strfilepath, intpos, 1) = ":" Then
Getfilename = right (strfilepath, Len (strfilepath)-intpos)
Exit Function
End if
Next
End Function
Private function checkfileextension (strfilename) as Boolean
Dim strfileextension as string
If instr (strfilename, ".") then
Strfileextension = mid (strfilename, limit Rev (strfilename, ".") + 1)
If Len (strfileextension) <3 then
Checkfileextension = false
Else
Checkfileextension = true
End if
Else
Checkfileextension = false
End if
End Function
Private sub writefile (byval struploadpath as string, byval strfilename as string ,_
Byval lngfilelength as long)
End sub
Public Function doupload (byval lngmaxfilebytes as long ,_
Byval struploadpath as string) as Variant
Dim varbytecount as Variant
Dim varhttpheader as Variant
Dim lngfilelength as long
Dim arrerror (0, 1) as Variant
On Error goto doupload_err
Varbytecount = myrequest. totalbytes
Varhttpheader = strconv (myrequest. binaryread (varbytecount), vbunicode)
Myresponse. Write varhttpheader
Dim intformfieldcounter as integer
Intformfieldcounter = Len (varhttpheader)-len (replace (varhttpheader, "; name =", mid ("; name =", 2 )))
Redim arrformfields (intformfieldcounter-1, 1) as Variant
For I = 0 to intformfieldcounter-1
Lngformfieldnamestart = release B (lngformfieldnamestart + 1, varhttpheader, "; name =" & CHR (34 ))
Lngformfieldnameend = 1_ B (lngformfieldnamestart + _
Len (strconv ("; name =" & CHR (34), vbunicode), varhttpheader, CHR (34 ))_
+ Len (strconv (CHR (34), vbunicode ))
Strformfieldname = midb (varhttpheader, lngformfieldnamestart, lngformfieldnameend-lngformfieldnamestart)
Strformfieldname = Replace (strformfieldname, "; name =", vbnullstring)
Strformfieldname = Replace (strformfieldname, CHR (34), vbnullstring)
If midb (varhttpheader, lngformfieldnameend, 2) = ";" then
Lngformfieldvaluestart = bytes B (lngformfieldnameend, varhttpheader, "filename =" & CHR (34 ))
Lngformfieldvalueend = bytes B (lngformfieldvaluestart + Len (strconv ("filename =" & CHR (34), vbunicode), varhttpheader, CHR (34 ))
Strfilename = midb (varhttpheader, lngformfieldvaluestart, lngformfieldvalueend-lngformfieldvaluestart)
Strfilename = mid (strfilename, instr (strfilename, "=") + 2, Len (strfilename)-instr (strfilename, "= "))
Strfilename = Replace (strfilename, CHR (34), vbnullstring)
Else
Lngformfieldvaluestart = lngformfieldnameend
Lngformfieldvalueend = bytes B (lngformfieldvaluestart, varhttpheader, vardelimeter)
Strformfieldvalue = midb (varhttpheader, lngformfieldvaluestart, lngformfieldvalueend-lngformfieldvaluestart)
Strformfieldvalue = Replace (strformfieldvalue, vbcrlf, vbnullstring)
Lngformfieldnamestart = lngformfieldvalueend
End if
Arrformfields (I, 0) = strformfieldname
Arrformfields (I, 1) = strformfieldvalue
Strfilename = getfilename (strfilename)
If Len (strfilename) = 0 then
Err. Raise err_no_filename
End if
If not checkfileextension (strfilename) then
Err. Raise err_no_extension
End if
Lngfiledatastart = instr (varhttpheader, strfilename), varhttpheader, vbcrlf & vbcrlf) + 4
Lngfiledataend = instr (lngfiledatastart, varhttpheader, vardelimeter)
Lngfilelength = lngfiledataend-lngfiledatastart
If lngfilelength <= 2 then
Err. Raise err_empty_file
End if
If not lngmaxfilebytes = 0 then
If lngmaxfilebytes <lngfilelength then
Err. Raise err_filesize_not_allowed
End if
End if
If not fs. folderexists (struploadpath) then
Err. Raise err_folder_does_not_exist
End if
If fs. fileexists (struploadpath & strfilename) then
Err. Raise err_file_already_exists
End if
Set sfile = FS. createtextfile (struploadpath & strfilename, true)
Sfile. Write varcontent, lngfiledatastart, lngfilelength
Close file
Sfile. Close
Set sfile = nothing
Set FS = nothing
Next
Doupload = ""
Exit Function
Doupload_err:
Arrerror (0, 0) = "error"
Select case err. Number
Case err_no_filename
Arrerror (0, 1) = "no file name to be submitted is entered. "
Case err_no_extension
Arrerror (0, 1) = "File Extension error. "
Case err_empty_file
Arrerror (0, 1) = "the length of the file to be uploaded is 0. "
Case err_filesize_not_allowed
Arrerror (0, 1) = "total to upload [" & lngfilelength &_
"] The Byte exceeds the maximum allowed value ["&_
Lngmaxfilebytes & "]."
Case err_folder_does_not_exist
Arrerror (0, 1) = "The uploaded directory does not exist. "
Case err_file_already_exists
Arrerror (0, 1) = "file [" & strfilename & "] already exists. "
Case else
Arrerror (0, 1) = err. Description
End select
Doupload = arrerror ()
End Function
Previous documents-comparison and description of the File Upload Component
Keywords:ASP
This section describes three commonly used Upload components:
All three components allow users to upload files using ie3.02 or later and netscape2.0 or later versions.
1. Microsoft Posting Acceptor component
This component uses ISAPI, a non-registered DLL. After the form is submitted, it is sent to this DLL.
You can write files to a specified directory and redirect them to the next page.
Of course, you must write
The directory has the write permission, so it is generally used to put the program passed in Win95 + PWS to NT.
An error occurs because it does not understand the permissions of NT and the SSL mechanism. This means that not everyone can
No one can upload files at will.
Second, it does not support writing files to the database. So if you want to have this function, you need
Use VB6 to develop your own components.
In addition, it does not help much. You cannot limit the size of the uploaded file and set the user's permissions.
In short, it can not only save files, but also has nothing to do with it.
2. Persits software's aspupload component
This is a powerful COM component, but you need to pay for it if you want to use its full version.
It provides the following functions:
A. restrict the size of the uploaded file
B. Set User Permissions
C. Modify file attributes
D. upload multiple files simultaneously
E. Saving files to the database
F. Supports file deletion. Files with different names are automatically generated from files on the server.
G. Users with administrative permissions can even use this control for remote registration.
3. Sa-fileup component of software artisans
This is the most expensive and powerful File Upload Component.
Its full version has the following features:
1. Complete documentation, including rich example programs
2. Provides a complete security mechanism for file uploading.
3. Writes data to the database using ADO. It also supports VB web class
Summary:
Feature Posting Acceptor aspupload Sa-fileup
Free for a single user $99 $129
Full free $300 $1,999
Simple Form submission Yes yes
Multifile upload No Yes
Integration with ASP No Yes
Whether the file can be processed No Yes
Whether database insertion is supported. No Yes
Support for ADO No Yes No
Whether ACL processing is performed. No Yes
Whether file encryption is supported. No Yes
Whether automatic No Yes installation is supported
Online Help is rarely used in many aspects
There are few examples
Little help online
Suggestion:
1. If you only want to practice, you can use Posting Acceptor
2. If you want to implement a solution for your website, use aspupload or SA-fileup.
Programming by yourself