Recently in a unit to do a project need to have file upload function, just at the hand of a book of the attached CD-ROM has an upload component, Heart exultation. Unfortunately, it was found that this is a trial version, do not give money can only use 50 times, too stingy!! Fortunately, the leader is not tight, time is ample, simply try to do one, can be long experience value, but also long sense of achievement, do not try white not to try! The following is the introduction of my two days and a half of the tinkering out of a can only be said to be embryonic things, just 30 lines of code, heroes do not laugh at me oh, this is only for the same curiosity with me, but the experience of the low value of the brothers to see.
The first step required for file uploads is, of course, to enter a page with a "file" form element, which is not mentioned, as we all know. But if you want to make a component that accepts the file and saves it to the server's hard disk, you must know how the original data is submitted. It is not difficult to do this:
<%
Dim formdata,formsize
Formsize=request.totalbytes
Formdata=request.binaryread (Formsize)
Response.BinaryWrite FormData
%>
Because you need to read the raw byte data submitted by the previous page, you cannot use the Request.Form () method, you must use the BinaryRead method, and once you use this method, you cannot use the Form () method on the same page, and vice versa. For the same reason, you must also use the BinaryWrite method when exporting to the page, but this does not prevent you from continuing to use the Response.Write method.
In the description below, suppose that the viewer chooses to upload a file that is an GIF image, and you find that the code does not display the image in the browser, but simply prints out a bunch of characters that are basically messed up:
gif89a 垐??!? 倇 挌 7 昳丽 € 聉}$ Dayton 藶 Platoon pai what to climb ⒖ to the of the North 熍 a 皗, and , 獂 the Bo 姰 畾 the 棩 ゥ of Dayton 藶 Platoon pai Water climbing ⒖, , North 熍 皗 獂, 姰, 畾, , 棩 ゥゥ 挗 | Deli 櫇 潒 Deli-嚈----杽 z 剟 剠 厁 儍 t??p{{{ Zzszzlsss 媙 arrj 僯 jrre€ccfff 乨? IIB??? W::aaa? S;? -----------------------------7ce20d227c0232--
No wonder, that's right! Because the raw data is not just the image file itself data, it also contains the file on the user's hard disk path, type, submit the form of the page, and other related information description, as well as the basic two paragraph of the identity (that is, "----------------------------- 7ce20d227c0232 "), I think this pair of logos should be split when the form contains multiple elements. The middle piece of data, which starts with "gif89a" until the Last ";", is the data of the GIF image file that we want to get. If you want to allow the browser to display the picture, of course, you have to remove the unwanted content and then to the page output. So the next thing to do is clear.
The first step, pinch the head:
"Header" and picture data dividing line is two to return line break (in the browser is not see "gif89a" before the blank line, you have to paste the output into WordPad to study, Notepad also not).
Dim Datastart,clstr
CLSTR=CHRB (+) & ChrB (10)
DATASTART=INSTRB (Formdata,clstr & Clstr) +4
4 of which is the length of the two pairs of carriage return line breaks, and also note that this is done for byte data, not characters, so do not use the wrong function, less knock a "B" is completely not the correct result.
Step two, go to the end:
How big is the "head" figure out, that end? No hurry, first get the specific content of the initial section of the logo, note that it ends with a pair of carriage return line breaks:
Dim Divstr
Divstr=leftb (FORMDATA,INSTRB (FORMDATA,CLSTR)-1)
Because we're going to use the MidB method to intercept the image data, we need to figure out the number of bytes in the entire picture data, using the DataSize variables defined previously:
DATASIZE=INSTRB (DATASTART+1,FORMDATA,DIVSTR)-datastart-2
Why do you want to lose 2? Because there is also a pair of carriage return line breaks before the end of the label. At this point, the picture data can be easily obtained and exported on the browser:
FORMDATA=MIDB (Formdata,datastart,datasize)
Response.BinaryWrite FormData
But I think you will not be satisfied with the end of it, because it has not been filed! Of course I would not have done it here, or I would not have been able to work for the boss. All of the things discussed above are limited to purely ASP scripting and do not involve component technology. In fact, I did not want to the beginning of this effort, just want to rely on ASP to solve the problem, but FileSystemObject didn't tell me it can write binary files on the hard disk, in this forum asked for a long time also no one to me, had to organize their own pieces.
But the next process is not as difficult as it might seem.
The functionality of this component is simple and provides only one method: SaveFile (Pathname), in which the program first obtains the byte data of the file submitted by the upload page, as described in the previous script, And then write the data to the server hard disk by the parameter pathname specified in a binary file, everything will be done, as to how to make this component further improve some, oh, later slowly.
How do you do it specifically? Open VB, and then listen to let's.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.