http://www.blueidea.com/bbs/archivecontent.asp?id=880985事实上是xmlhttp技术来上传文件。只要构造了正确的提交格式,文件就传上去了。如果服务器端是php的,你需要做如下改动。$d = "d:/" ;
copy ($_FILES['myfile']['tmp_name'], $d.$_FILES['myfile']['name']);
copy ($_FILES['myfile']['tmp_name'], $d.$_FILES['myfile']['name']);
Content-Disposition: form-data; name="email"
[email protected]
-----------------------------7cf87224d2020a
Content-Disposition: form-data; name="blob"; filename="c:image.gif"
Content-Type: image/pjpeg -----------------------------7cf87224d2020a
Content-Disposition: form-data; name="Enter"
Submit Query
-----------------------------7cf87224d2020a--
--------------------
对比下小疯狗写的那个xmlhttp上传示范,你可以发现奥秘就在于他将数据编码并重新组合了。alert下提交的数据,你可以发现一堆二进制编码。
<script language="javascript">
function loadHttpXML(url, sendObj, doFunction, method)
{
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP")
var xmldoc = new ActiveXObject("Msxml2.DOMDocument")
xmldoc.async = false
xmldoc.resolveExternals = false
var method = method ? method : "post"
if (doFunction) xmlhttp.onreadystatechange = xmlhttpReadychange
xmlhttp.open(method, url, false)
//xmlhttp.setRequestHeader("content-type", "application/x-www-form-urlencoded")
xmlhttp.setRequestHeader("content-type", document.all["contentType"].value)
xmlhttp.send(sendObj) if (!doFunction)
{
xmldoc.loadXML(xmlhttp.responseText)
return xmldoc
} function xmlhttpReadychange()
{
if (xmlhttp.readyState == 4)
{
xmldoc.loadXML(xmlhttp.responseText)
doFunction(xmldoc)
//doFunction(xmlhttp.responseXML)
}
}
} function loadFile(path)
{
var stream = new ActiveXObject("ADODB.Stream")
stream.Type = 1
stream.Open()
stream.LoadFromFile(path)
var binaryStream = stream.Read()
stream.close()
return binaryStream
} function send(form)
{
var formDate = form.sendString.value ? form.sendString.value : alert("请选择文件")
sendbinary = hexToByte(strToHex(formDate.split("<!--file-->")[0]) + byteToHex(loadFile(document.all["picPath"].value)) + strToHex(formDate.split("<!--file-->")[1]))
loadHttpXML(form.action, sendbinary)
pic.src = "myphoto"
}
</script><script language="vbscript">
function strToHex(str)
dim i, char, hexValue
for i=1 to len(str)
char = hex(asc(mid(str, i, 1)))
if len(char) = 1 then : char = "0" & char : end if
hexValue = hexValue & char
next strToHex = hexValue
end function function hexTostr(hexStr)
dim i, char, hexValue
for i=1 to len(hexStr) step 2
char = mid(hexStr, i, 2)
if char > "80" then
i = i+2
char = char & mid(hexStr, i, 2)
end if
Execute "char = &H" & char
hexValue = hexValue & chr(char)
next HexTostr = hexValue
end function
</script><script language="javascript">
function byteToHex(byteStr)
{
var xmldom = new ActiveXObject("Microsoft.XMLDOM")
var byteObj= xmldom.createElement("byteObj")
byteObj.dataType = 'bin.hex'
byteObj.nodeTypedValue = byteStr
return(String(byteObj.text))
} function hexToByte(hexStr)
{
var xmldom = new ActiveXObject("Microsoft.XMLDOM")
var byteObj= xmldom.createElement("byteObj")
byteObj.dataType = "bin.hex"
byteObj.nodeTypedValue = hexStr
return(byteObj.nodeTypedValue)
} function strToByte(str)
{
return hexToByte(strToHex(str))
} function byteToStr(byteStr)
{
return hexTostr(byteToHex(byteStr))
}
</script><form action="xmlhttp.php" onsubmit="event.returnValue = false; send(this)">
<input id="picPath" type="file" size="50" /><br />
<input type=text id=contentType value="multipart/form-data, boundary=AaB03x" size="50"><br />
<textarea id=sendString rows=30 cols=49>
--AaB03x
content-disposition: form-data; name="user"Wilson Peng
--AaB03x
content-disposition: form-data; name="myfile"; filename="myphoto"
Content-Transfer-Encoding: binary<!--file-->
--AaB03x
</textarea>
<input type="submit" value="send"/>
</form><img id="pic" src="myphoto" />