Upload (because I haven't finished my component yet, I don't guarantee the correctness of the code in the article.)
Upload Multiple Files
(Sander Duivestein)
Introduced
This is an ActiveX component of the upload file I wrote. This component uses the WinSocket control to invoke FTP to submit files. It works only in Ie3.02 or higher versions, but some clients use Netscape, so they have to find another solution. But I search The Internet and a number of newsgroups, I found more than one of the requirements.
In 1999.3.11, I saw a 15Seconds article by Doug Dean (about uploading files with VB ASP components) gave me a good tip.
Doug Dean's components are simple and easy to use. But he said, however, the issue of multiple component uploads did not solve? There is still some work to be done.
Before I started working on my own components, I wanted to know what other similar controls provided. So I looked at the other 3 famous components:
The upload component of Software artisans, the upload component of AspUpload,
And the Microsoft Posting acceptor.
By comparing these components I think my components should meet the following requirements:
The HTML form for submitting the file should be a black box for the ASP component. That is, the component can accept various table elements and can get the name and value of the form element.
It should be able to provide an upload path and limit the size.
The component should be able to handle multiple files.
Component should have an error handler.
Components should be well performing.
Components should be able to work like IE in NC.
Save the file into the database.
Allow only one group to upload files.
These are quite a challenge for me.
Solve the problem
First I'm going to create an HTML file that contains two components: a simple text box, a file box. Here's the following code:
1:upload.htm
<HTML>
<HEAD><TITLE>Upload</TITLE></HEAD>
<BODY>
<form name= "Frmupload" method= "Post" enctype= "Multipart/form-data" action= "upload.asp" > <TABLE>
<tr><td>author</td><td><input type= "text" name= "Txtauthor" ></TD></TR>
<tr><td>file</td><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>
Use enctype= "Multipart/form-data" to enable the form to submit a file. We also need a file to receive files.
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
Response.Write "The component wasn ' t registered"
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
%>
Set the following two variables here:
Lngmaxfilebytes-File Maximum bytes, and struploadpath-file upload location. I also added an error handler to check whether the component is properly registered on the network server. This is the only mistake I've made to deal with. If any other error occurs, Can be added to deal with it. Finally, declare Varreturn. This variable is used to accept the return value of the component. This return value should contain all the table cell names and their values. You can see the program in the For NEXT loop, this return value must be an array.
This is a relatively easy part. Now we have to create an ActiveX component to process the submitted form.
Open VB6 and select an ActiveX project (see Step 1:)
Step 1:
Create an ActiveX DLL project
First, add a reference, select the Add Reference item on the menu bar, and check
Active Server Pages Object Library. (See step 2).
Step 2:
Project References
Through this library we can use the requested object of the ASP's request. To make sure you can use it, use the following code:
Option Explicit
Private Myscriptingcontext as ScriptingContext
Private Myrequest as Request
Private Myresponse as Request
Public Sub OnStartPage (Passedscriptingcontext as ScriptingContext)
Set Myscriptingcontext = Passedscriptingcontext
Set myrequest = myscriptingcontext.request
Set Myresponse = Mysriptingcontext.response
End Sub
Why do we need an ASP library? Through the Request object we can get the HTTP data stream from Upload.htm. Why is there a "but" in there? When we try to read the form field name and the relative value, for example, Request.Form ("Txttitle"), But we can't read the rest of the raw data sent to us. So we use request.totalbytes and request.binaryread to read the sent data.
Here's the code I got from Doug Dean:
' ~~~~~ VARIABLES ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dim Varbytecount
Dim BinArray () as Byte
' ~~~~~ BYTE COUNT of RAW FORM DATA ~~~~~~~~~~~~
&nb