Ajax upload (XMLHTTP File Upload exceeds the size limit)

Source: Internet
Author: User
Transfer Ajax upload (XMLHTTP File Upload exceeds the size limit)

<HTML>
<Head> <title> File Upload </title>
</Head>
<Body>
<Form name = "Upload">
<Table width = "100%" border = "0" cellpadding = "5" cellspacing = "0">
<Tr>
<TD colspan = "2"> upload prompt: Microsoft ADO/rds
This page is accessing data from other domains. Can you do this? </TD>
</Tr>
<Tr>
<TD width = "100"> your image </TD>
<TD> <input name = "pic" type = "file" size = "20"> </TD>
</Tr>
<Tr>
<TD colspan = "2" align = "center"> <input id = "upbtn" type = button value = "/blog/upload"> </TD>
</Tr>
</Table>
</Form>

</Body>
</Html>

<Script language = JavaScript>
VaR XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");

VaR St, up
Try {
St = new activexobject ("ADODB. Stream ");
Up = true
}
Catch (e ){
Up = false
}
Function upload. upbtn. onclick (){
VaR upic = upload. PIC. Value
If (validpic ()! = "") Alert (validpic ())
Else {
If (! Up) Alert ("your browser version is too low to upload images. Please upgrade your browser! ")
Else {
St. type = 1;
St. open ();
Try {
St. loadfromfile (upic );
VaR DB = ST. Read ()
St. Close ()
VaR txtp = splitstring (bytetostr (DB), 100000) // here we cut the file that has been converted into a string. I cut it by 0.1 million bytes, that is, 50 kb.
VaR xmldoc = new activexobject ("Microsoft. xmldom ");
Xmldoc. loadxml ('<? XML version = "1.0"?> <Root/> ');
For (VAR I = 0; I <txtp. length; I ++ ){
VaR ipic = xmldoc. createelement ("pic ")
Ipic. Text = txtp [I];
Xmldoc.doc umentelement. appendchild (ipic );
}
XMLHTTP. Open ("Post", "Upload. asp", true );
XMLHTTP. onreadystatechange = rep
XMLHTTP. Send (xmldoc );
}
Catch (e) {alert ("the file cannot be found! \ N "+ E. Description )}
}
}
}

Function rep (){
If (XMLHTTP. readystate = 4 ){
If (XMLHTTP. Status = 200 ){
Alert (XMLHTTP. responsebody)
Location. Reload ()
} Else
Alert ("status:" + XMLHTTP. Status + "\ n" + XMLHTTP. statustext)
}
}

Function validpic (){
VaR maxsize = 500 // maximum size limit
VaR STR =''
VaR thispic = new image ();
Thispic. src = upload. PIC. Value
If (thispic. src = "") STR = 'select the image you want to upload'
Else {
VaR picsize = thispic. filesize/1024
If (picsize> maxsize) STR = 'upload images must not be larger than '+ maxsize +' kb. Your images include '+ picsize +' kb'
Else {
VaR spic = thispic. SRC. Split (".")
VaR pF = spic [spic. Length-1]. tolowercase ()
STR = 'unknown image format! '
VaR picf = new array ("GIF", "jpg", "Jpeg", "BMP ")
For (VAR I = 0; I <picf. length; I ++ ){
If (pF = picf [I]) {
STR =''
Break
}
}
}
}
Return Str
}

// Convert a binary file to a string
Function bytetostr (p ){
VaR xmldom = new activexobject ("Microsoft. xmldom ");
Xmldom. async = false
Xmldom. loadxml ('<? XML version = "1.0"?> <Root/> ');
VaR tpic = xmldom. createelement ("pic ")
Tpic. datatype = 'bin. hex'
Tpic. nodetypedvalue = P
Xmldom.doc umentelement. appendchild (tpic );
Return (string (tpic. Text ))
}

// Cut the string according to the desired length
Function splitstring (S, L ){
VaR H = S. Length
VaR Al = (S. Length % L = 0 )? H/L: parseint (string (S. Length/L). Split (".") [0]) + 1
VaR A = new array (Al)
For (VAR I = 0; I <A. length; I ++)
A [I] = S. substring (I * l, l * (I + 1 ))
Return ()
}
</SCRIPT>

Upload. asp
<% @ Language = VBScript %>
<% Option explicit %>
<%
Response. contenttype = "text/XML"
Response. charset = "UTF-8"
Dim xmldom
Dim root, PIC
Set xmldom = server. Createobject ("Microsoft. xmldom ")
Xmldom. async = false
Xmldom. Load request
If xmldom. parseerror. errorcode <> 0 then
Response. binarywrite "Unknown error! "
Else
If xmldom. readystate = 4 then
Xmldom. Save server. mappath ("image. xml ")
Response. binarywrite "Upload successful! "
'If you want to directly store the data in the database, That's all:
'Dim I, A, txtp, bytep
'Set A = xmldom.doc umentelement. selectnodes ("/root/PIC ")
'Txtp = ""
'For I = 0 to A. Length-1
'Txtp = txtp & A (I). Text
'Next
'Bytep = strtobyte (txtp) 'This is what you want
End if
End if
%>
<Script language = "JavaScript" runat = "server">
Function strtobyte (STR ){
VaR xmldom = server. Createobject ("Microsoft. xmldom ")
Xmldom. loadxml ('<? XML version = "1.0"?> <Root/> ')
VaR bpic = xmldom. createelement ("pic ")
Bpic. datatype = "bin. Hex"
Bpic. nodetypedvalue = Str
Xmldom.doc umentelement. appendchild (bpic)
Return (bpic. nodetypedvalue)
}
</SCRIPT>

Image. asp
<% @ Language = JScript %>
<%
VaR xmldoc = new activexobject ("Microsoft. xmldom ")
Xmldoc. async = false
Xmldoc. Load (server. mappath ("image. xml "))
If (xmldoc. parseerror. errorcode! = 0 ){
Response. Write ("error ")
} Else {
VaR A = xmldoc.doc umentelement. selectnodes ("/root/PIC ")
VaR PIC =''
For (VAR I = 0; I <A. length; I ++)
PIC + = A [I]. Text // here, the data is reorganized.
Response. binarywrite (strtobyte (PIC ))
}

// Convert a string to a binary stream
Function strtobyte (STR ){
VaR xmldoc = server. Createobject ("Microsoft. xmldom ")
Xmldoc. loadxml ('<? XML version = "1.0"?> <Root/> ')
VaR bpic = xmldoc. createelement ("pic ")
Bpic. datatype = "bin. Hex"
Bpic. nodetypedvalue = Str
Xmldoc.doc umentelement. appendchild (bpic)
Return (bpic. nodetypedvalue)
}
%>

Related Article

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.