First, the principle of unblented upload

xiaoxiao2021-03-05  49

Source: Baoyu Blog 1, I don't have one instance of the principle, I still use an example, the client HTML is as follows. To browse uploads, we pass

Elements, but must pay attention to you must set the form's encType property as "Multipart / Form-Data":

In the background ASP program, the ASCII data submitted before the form is very easy. However, if you need to get uploaded files, you must read it using the binaryRead method of the Request object. BinaryRead method is a binary reading of the current input stream to specify the number of bytes. It is a bit necessary to note that once the binaryRead method is used, it cannot be used with the request.form or request.QueryString collection. Combined with the Totalbytes property of the Request object, all of the data submitted by all forms can be made into binary, but the data is encoded. Let us first take a look at how these data is encoded, there is no laws, and the code code, in the code, we convert binaryRead to the binary to text, output, in the background of UPLOAD.ASP (note This example does not go upload big files, otherwise it may cause browsers to die): <% Dim Bidata, postData size = request.totalbytes bidata = request.binaryTost (size) PostData = binarytotring (bidata, size) response.write

"& PostData &" "Use pre-output format 'to convert binary stream into text Function binarytoString (BiData, Size) const adLongvarchar = 201 set RS = CreateObject (" AdoDb.Recordset ") rs.fields.Append" Mbinary ", Adlongvarchar, Size Rs.open Rs.addNew RS (" mbinary "). Appendchunk (bidata) rs.Update binarytostring = rs (" mbinary "). Value rs.close end function%> Simple start, upload a simplest Text file (G: /HomePage.txt, content is "Baoyu: http://www.webuc.net") Test, the text box filename retains the default value "default filename", submits the output of the output: - --------------------------- 7D429871607FE Content-disposition: form-data; name = "file1"; filename = "g: / homepage. TXT "Content-Type: TEXT / Plain] http://www.webuc.net ---------------------------- 7D429871607FE Content-disposition: form-data; name = "filename" default filename ---------------------------- 7D429871607FE - can see For items in the form, it is used "-------------------------- 7D429871607FE" to separate into a piece of one There are some description information, such as content-disposition: form-data; name = "filename", in the description information, you can know the Name of the form by Name = "filename". If there is filename = " G: /Homepage.txt "This kind of content, explanation is an uploaded file, if it is an uploaded file Then the description information will multi-Type: Text / Plain to describe the content-type of the file. The description information and main body information are separated by a wrap.

Well, it is basically clear. According to this rule, we know how to separate the data, and then process the separated data, but almost ignore a problem, which is boundary value (in the previous example ------- ---------------------- 7D429871607FE ") How do you know? Every time you upload this boundary value is different, it is also good, and the ASP can be obtained through the request.servervariables ("http_content_type"), such as the HTTP_CONTENT_TYPE content in the upper example: "Multipart / Form-data; boundary = ------------------------ 7D429871607FE ", there is this, we can not only determine if there is an encType =" Multipart / Form- Data "(if not used, then there is no need to execute below), you can also get boundary value boundary = ------------------------- 7D429871607FE. (Note: The boundary value obtained here is less "-", it is best to add more than the boundary value above.) As for how to analyze data, I don't have to repeat it, nothing more than the means of INSTR, MID, etc. To separate the data we want. Second, block upload, record progress to reflect the progress bar in real time, the essence is to know how much data has been obtained in real time? Let me recall the process we implemented, we are implemented via request.binaryRead (Request.TotalBytes), we cannot know how much data obtained by the current server in the process of Request.TotalBytes. Therefore, it can only be changed, if we can divide the obtained data into a piece, then according to the number of blocks already uploaded, we can calculate how big it is currently uploaded! That is, if I am 1K is 1, then upload 1MB input stream is divided into 1024 blocks. For example, I have now got 100 blocks, then it is currently uploaded 100K. When I proposed a block, many people think it is incredible because they all ignore the binaryRead method not only can read the specified size, but also can be read continuously.

Write an example to verify the integrity of the block read, on the basis of just the example (Note that this example does not go upload big files, otherwise it may cause the browser to die): <% Dim Bidata, PostData, Totalbytes, Chunkbytes Chunkbytes = 1 * 1024 'Piece size of 1k totalbytes = request.totalbytes' total size PostData = ""' Translated into text type after the text type, readedbytes = 0 'initialized to 0' block read do while ReadedBytes TotalBytes Then ReadedBytes = TotalBytes Loop Response.Write "

"& PostData &" "Use pre-output format 'to convert binary stream into text Function BinaryTostring (Bidata, size) const adlongvarchar = 201 set = createObject (" AdoDb.Recordset ") rs.fields.Append" mbinary ", Adlongvarchar, Size Rs.open Rs.addnew RS ("mbinary"). Appendchunk (bidata) rs.Update binarytostring = rs ("mbinary"). Value rs.close end function%> Tests the text file uploaded, output results Prove that the contents of this partial read are complete, and in the While loop, we can record the current state to the Application while cycles, and then we can access the Application to dynamically obtain the uploading strip. In addition: In the case, it is spliced ​​by a string. Binary Do While ReadedBytes TotalBytes Then ReadedBytes = TotalBytes Application ( "ReadedBytes") = ReadedBytes Loop 3, saving uploaded files Get submitted data via request.binaryRead Binary flow preservation becomes a document. For text data, you can pass Texts The WRITE method of the Tream object saves text data to the file. For text data and binary data, it is convenient to convert each other. For uploading small files, the two are basically no difference. However, there are still some differences in two ways. For AdoDB.Stream objects, all data must be loaded until you can save it, so use this way. If the uploaded large file will take place, for the TextStream object, After the file is created, a part of the Write is part of the WRITE, which is the benefit of the server memory space, combined with the block acquisition data principle, we can get a write of uploaded data Write to the file in. I have done a test, and the machine has been uploaded up to more than 200 MB files. I used the first way to use the memory has been rising. I finally prompted that the computer virtual memory is insufficient. The most hate is even if the progress bar indicates that the file has been uploaded, but finally The file is still not saved.

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

New Post(0)