Shortest component upload code (only 9 lines)

Source: Internet
Author: User
Tags header
Upload | No components I studied the no component upload, and made one of the simplest possible ways
----Use 9 lines of code to truly implement no component upload, how to say that spent half a day to write 9 lines of code is a bit of efficiency and results.
So I wrote this article.



Formsize=request.totalbytes ()
Formdata=request.binaryread (Formsize)
Dataheader=leftb (FORMDATA,INSTRB (FORMDATA,CHRB) &CHRB (10))
Strfiledata=midb (Formdata,lenb (Dataheader), LenB (FormData)-lenb (dataheader) *2-3)
HEADERPOS2=INSTRB (STRFILEDATA,CHRB (AscB ("-")) &chrb (AscB ("T")) &chrb (AscB ("Y")) &chrb (AscB ("P")) & C

HrB (AscB ("E"))
HEADERPOS3=INSTRB (HEADERPOS2,STRFILEDATA,CHRB (13))
DATAHEADER2=LEFTB (STRFILEDATA,HEADERPOS3+4)
Strfiledata=midb (Strfiledata,lenb (DataHeader2), LenB (Strfiledata))
Response.BinaryWrite (Strfiledata)







dataheader--file Header
strfiledata--the data after the header of the file, the final output value
Headerpos2--content-type: the location of
headerpos3--Example: Location of Content-type:image/gif
dataheader2--the entire front of the file header

Thought:
1. Remove file headers and tails first (e.g.-----------------------------7d230d1f940277)
2. Skip all previous information directly to find Content-type:image/gif
3. Extract useful information and fields
4. The data has come up and how to save it is simple. As for the FSO or ADODB.stream, it's up to you.

Problem:
One might say that a form cannot have only one file
That way, your data can't be extracted in such a simple way.
In fact, it's better if you separate them.


Here is the full example

Uup.htm
<a href= "http://www.cgfront.com" >www.cgfront.com</a>
<form name= "LoadFromFile" action= "uup.asp" method= "post" enctype= "Multipart/form-data" >
<input type= "Submit" name= "Submit" value= "OK" >
<input type= "File" Name= "LoadFromFile" style= "width:400" "value=" ">
</form>

Uup.asp
<%
Formsize=request.totalbytes ()
Formdata=request.binaryread (Formsize)
Dataheader=leftb (FORMDATA,INSTRB (FORMDATA,CHRB) &AMP;CHRB (10))
Strfiledata=midb (Formdata,lenb (Dataheader), LenB (FormData)-lenb (dataheader) *2-3)
HEADERPOS2=INSTRB (STRFILEDATA,CHRB (AscB ("-")) &chrb (AscB ("T")) &chrb (AscB ("Y")) &chrb (AscB ("P")) & C

HrB (AscB ("E"))
HEADERPOS3=INSTRB (HEADERPOS2,STRFILEDATA,CHRB (13))
DATAHEADER2=LEFTB (STRFILEDATA,HEADERPOS3+4)
Strfiledata=midb (Strfiledata,lenb (DataHeader2), LenB (Strfiledata))
Response.BinaryWrite (Strfiledata)
%>




If you press the CTRL + C +V key on your keyboard has been broken by you, it doesn't matter I also provide you with the corresponding test site

Http://www.cgfront.com/up/uup.htm

Finally, I hope we can teach you a lot.


Author: Leonard



Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.