轉
ajax上傳(xmlhttp上傳檔案突破大小限制)
<HTML>
<HEAD><TITLE>File upload</TITLE>
</HEAD>
<BODY>
<form name="upload">
<table width="100%" border="0" cellpadding="5" cellspacing="0">
<tr>
<td colspan="2">上傳提示:Microsoft ADO/RDS
此頁正在訪問其他域的資料。您允許這樣做嗎?</td>
</tr>
<tr>
<td width="100">您的圖片</td>
<td><input name="pic" type="file" size="20"></td>
</tr>
<tr>
<td colspan="2" align="center"><input id="upbtn" type=button value="/blog/上傳"></td>
</tr>
</table>
</form>
<img src="/blog/image.asp" border="0">
</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("您的瀏覽器版本太低,無法上傳圖片,請升級您的瀏覽器!")
else{
st.Type = 1;
st.Open();
try{
st.LoadFromFile(uPic);
var db = st.Read()
st.close()
var txtP = splitString(ByteToStr(db),100000)//這裡把已經轉換成字串的檔案切割一下,我是按10萬位元組長度切割的,也就是50KB
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.documentElement.appendChild(iPic);
}
xmlhttp.open("POST","upload.asp",true);
xmlhttp.onreadystatechange = rep
xmlhttp.send(xmldoc);
}
catch(e){alert("無法找到檔案!\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//最大的尺寸限制
var str = ''
var thisPic = new Image();
thisPic.src = upload.pic.value
if(thisPic.src=="") str = '請選擇您要上傳的圖片'
else{
var picSize = thisPic.fileSize/1024
if(picSize>maxSize) str = '上傳的圖片不要大於'+maxSize+'KB,您的圖片有'+picSize+'KB'
else{
var sPic = thisPic.src.split(".")
var pf = sPic[sPic.length-1].toLowerCase()
str = '未知的圖片格式!'
var picF = new Array("gif","jpg","jpeg","bmp")
for (var i=0;i<picF.length;i++){
if (pf==picF[i]){
str = ''
break
}
}
}
}
return str
}
//把二進位檔案轉換成字串
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.documentElement.appendChild(tPic);
return(String(tPic.text))
}
//把字串按你想要的長度切割
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(a)
}
</SCRIPT>
<img src="/blog/image.asp?id=0" border="0">
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 "未知的錯誤!"
else
if xmldom.readyState=4 then
xmldom.save Server.MapPath("image.xml")
Response.BinaryWrite "上傳成功!"
'如果想直接放到資料庫,就這麼著:
'dim i,a,txtP,byteP
'set a = xmldom.documentElement.selectNodes("/root/pic")
'txtP = ""
'for i=0 to a.length-1
'txtP = txtP & a(i).text
'next
'byteP = StrToByte(txtP)'這就是你要的東東
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.documentElement.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.documentElement.selectNodes("/root/pic")
var pic = ''
for (var i=0;i<a.length;i++)
pic += a[i].text//這裡就是把資料重組
Response.BinaryWrite(StrToByte(pic))
}
//把字串轉換成二進位流
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.documentElement.appendChild(bPic)
return(bPic.nodeTypedValue)
}
%>