upload.htm
<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)
}
%>
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)