Upload Next, create an ActiveX DLLs type of project, and the default project name from "Project1" to "Songbird" (Of course you also have the right not to change, hehe), the default class name from "Class1" to "Fileup", as to why this change , and you will know it later.
To realize the ability to read and upload file byte data, it is bound to involve the request object, but in VB how to do it? Simple, just add the appropriate type library reference to the project. Click the Project menu to select References, in the dialog box that appears, select the Microsoft Active Server Pages object and the Microsoft Transaction Server Type Library entry. Click OK, you can in the VB environment like writing ASP code, such as manipulation request, response, etc., you will feel very cordial.
The following is the specific program code:
Public Function SaveFile (Pathname As String) as String
OnError Goto ErrorCode
Dim Objcontext as ObjectContext
Dim Objrequest as Request
' ObjectContext's use is similar to the application and session objects in ASP,
' You can access some specific object instances within them with key values,
' such as ObjectContext ("Response")
Set Objcontext=getobjectcontext ()
Set objrequest=objcontext ("Request")
' The following code is similar to the script in the previous text,
' It's just a strict definition of a variable.
Dim FormData () as Byte,clstr,divstr
Dim Datastart as long,datasize as Long
Datasize=objrequest.totalbytes
Redim FormData (DataSize-1)
Formdata=objrequest.binaryread (DataSize)
CLSTR=CHRB (+) & ChrB (10)
DATASTART=INSTRB (Formdata,clstr & Clstr) +4
Divstr=leftb (FORMDATA,INSTRB (FORMDATA,CLSTR)-1)
DATASIZE=INSTRB (DATASTART+1,FORMDATA,DIVSTR)-datastart-2
FORMDATA=MIDB (Formdata,datastart,datasize)
' Create a binary file and write formdata to it
Open Pathname for Binary as 1
Put #1, FormData.
Close #1
Savefile= "ok!"
Exit Function
Errorcode:savefile=err.description
End Function
Done! The rest is how to use the program.
By clicking on the File menu and choosing Make Songbird.dll, the system compiles the program into a DLL file and automatically registers it on the computer. Unless you are developing directly on the server, you should copy this file to the NT Server's System32 directory and run the "regsvr32 Songbird.dll" command to register. After the registration is successful, this component is used in the ASP file that accepts the upload file:
<%
Dim Fu
Set fu=server.createobject ("Songbird.fileup")
' Accustomed to CreateObject (ADODB. Connection "),
' When I saw the component named after my own name, my heart was filled with infinite excitement,
' Haha haha! ... @#$%&*, sorry, lyrical seems not my strength, hehe
' Save the file to the server's D-Packing directory
Response.Write Fu. SaveFile ("D:\a.gif")
Set fu=nothing
%>
Ok! I hope what you see in the browser is "ok! ”
Note:
1, this component only realizes the most basic function, is not practical, oneself use, coax the boss also forget. The purpose of writing is simply to allow interested friends to share some of the things that are available.
2. When uploading files to NT servers, you must ensure that browsers have at least the right to add and change to the target folder, otherwise they will report "file Name or number Error".
3, in writing intercepts the file data the statement to be careful not to confuse the byte number, otherwise the program runs does not have the error, but the server receives is only a heap of useless bytes.
4, although this article is around to upload a GIF image file to expand the description, but no matter what other types of file upload the truth is the same, do not change the components of the source program, just use the component when the target file name to specify a correct extension can be.