ASP implementation

xiaoxiao2021-03-06  95

Write a website application with ASP, it is difficult to encounter a wide range of questions, where to upload files to the server, I am probabulous to meet the most problems, especially upload pictures, such as what you want in your own community. Realize the "Daily One Star" functionality provided by Netease Virtual Community, it is necessary to provide users' ability to upload photos. Upload image files to the server can use a variety of free file upload components, which is very powerful, but because in many cases, we can only use the free support ASP space or rent different virtual space for the first situation. We have no possibility to use file upload components; as for the second case, we have to pay a lot of "silver". Unless you have your own virtual host, you can just install your own components on the server, this situation is desirable for most people. Then there is no way? Oh, the answer is sure (of course, it is certain, or I can't write this article). Let's use the pure ASP code to implement the upload of the picture and save to the database. First let's first familiarize yourself with the object method to use. The data we use to get the previous page is generally used to use the Request object. Similarly, we can also use the REQUEST object to get uploaded file data, and the method used is request.binaryRead (). The data we have to read from the database is displayed to the method you want to use on the web page is: request.binaryWrite (). When we get the picture of the picture, when you want to save to the database, you cannot use the INSERT statement to operate directly using the ADO's Appendchunk method, the same, read the image data in the database, to use Getchunk method. The specific syntax of each method is as follows: * Request.binaryRead Syntax: Variant = Request.binaryRead (count) Parameter Variant Return Value saves to read the data from the client. Count indicates the amount of data to be read from the client, which is less than or equal to the amount of data obtained by using the method Request.Totalbytes. * Request.binaryWrite Syntax: Request.BinaryWriteData Parameters Data To write to the packet in the client browser. * Request.totalbytes syntax: variant = request.totalbytes parameter Variant Returns the number of bytes from the client to the amount of data. * Appendchunk syntax adds data to large text, binary data field or parameter object. Object.AppendchunkData Parameters ObjectField or Parameter object DATA variable body, contains data added to the object. Note The Appendchunk method using the field or parameter object can be filled in the object in the object. In the case where the system is limited, the Appendchunk method can be used to partially perform a portion rather than all operations. * Getchunk syntax returns all or part of the large text or binary data Field object. Variable = field.getchunk (size) Return value return variant. Parameter size long integer expressions, equal to the number of bytes or characters to be retrieved. Note The GetChunk method using the Field object retrieves its part or all long binary or character data. In the case where the system is limited, the GetChunk method can be used to handle the portion instead of all long integer values. The data returned by the getChunk call will assign it to the "variable".

If size is greater than the remaining data, getChunk only returns the remaining data without populating the "variable" with a blank. If the field is empty, the getChunk method returns NULL. Each subsequent GetChunk call will retrieve data starting from the previous GetChunk call stop. However, if the data is retrieved from a field and then set or read in the current record, the ADO will consider the data from the first field. If the getchunk method is called again on the first field, the ADO will interpret the call as a new GetChunk operation and start reading from the beginning of the record. If other Recordset objects are not a copy of the first RECORDSET object, the fields that are accessed do not destroy the getChunk operation. If the Adfldlong bit in the Attributes property of the Field object is set to True, you can use the getChunk method for this field. If there is no current record when using a getchunk method on the field object, an error 3021 (no current record) is generated. Next, we have to design our database, as a test of our database structure (Access2000): Field Name Type Description ID Auto Number Main key value IMG OLE object is used to save image data For MSSQLSERVER7, the corresponding structure As follows: Field Name Type Description ID INT (Identity) Primary key value IMG image Used to save image data Now officially preparing our pure ASP code upload section, first, we have an upload interface that provides users, allowing users to choose To upload the picture. The code is as follows:


Note EncType = "Multipart / Form-Data", be sure to have this attribute in Form Otherwise, the uploaded data will not be obtained. Next, we have to perform the necessary processing from the data obtained from the browser in process.asp, because the data we have obtained in Process.asp contains not only the data of the uploaded picture we want, Contains other useless information, we need to remove redundant data and save the handled picture data into the database, here we take Access 2000 as an example.

Specific code as follows (process.asp): <% response.buffer = trueformsize = request.totalbytesformdata = request.binaryread (formsize) bncrlf = chrB (13) & chrB (10) divider = leftB (formdata, clng (instrb (formdata, bncrlf )) - 1) datastart = instrb (formdata, bncrlf & bncrlf) 4dataend = instrb (datastart 1, formdata, divider) -datastartmydata = midb (formdata, datastart, dataend) setconnGraph = server.CreateObject ( "ADODB.connection") connGraph .ConnectionString = "driver = {MicrosoftAccessDriver (* mdb.)}; DBQ =" & server.MapPath ( "images.mdb") & "; uid =; PWD =;" connGraph.Opensetrec = server.createobject ( "ADODB.recordset ") Rec.Open" Select * from [images] whereidisnull ", conngraph, 1,3rec.addnewrec (" img "). appendchunkmydatarec.UpdateRec.clossetRec = NothingSetConngraph = Nothing%> Ok, let's take up this The picture saved in the database named Images.mdb, and the rest of the job is to display the image data in the database to the web page.

转载请注明原文地址:https://www.9cbs.com/read-105636.html

New Post(0)