使用asp和vb实现FileUpload的一种实现(XML)
首先是客户端的实现 ,CFileUpload使用Vbscript编写,在VB中同样测试通过
<script language="VBScript">
<!--
class CFileUpload
Private sFilePath
Private sUploadServer
Private sSaveFilePath
Private sFileName
Private sStatus
Private sResponseInfo
Public Property Let FilePath(ByVal vData)
sFilePath = vData
End Property
Public Property Get FilePath()
FilePath = sFilePath
End Property
Public Property Let FileName(ByVal vData)
sFileName = vData
End Property
Public Property Get FileName()
FileName = sFileName
End Property
Public Property Let SaveFilePath(ByVal vData)
sSaveFilePath = vData
End Property
Public Property Get SaveFilePath()
SaveFilePath = sSaveFilePath
End Property
Public Property Let UploadFileServer(ByVal vData)
sUploadServer = vData
End Property
Public Property Get UploadFileServer()
UploadFileServer = sUploadServer
End Property
Public Property Get ResponseStatus()
ResponseStatus = sStatus
End Property
Public Property Get ResponseInformation()
ResponseInformation = sResponseInfo
End Property
Public Sub UploadFile()
Dim adoStream
Dim xmlDOM
Dim objHTTP
Dim ndFileName
Dim ndFileData
Dim ndSaveFilePath
Set adoStream = CreateObject("ADODB.Stream")
Set xmlDOM = CreateObject("MSXML2.DOMDocument")
xmlDOM.loadXML ("<?xml version=""1.0"" ?><Root/>")
xmlDOM.documentElement.setAttribute "xmlns:dt", "urn:schemas-microsoft-com:datatypes"
Set ndFileName = xmlDOM.createElement("FileName")
ndFileName.Text = sFileName
xmlDOM.documentElement.appendChild (ndFileName)
Set ndSaveFilePath = xmlDOM.createElement("SaveFilePath")
ndSaveFilePath.Text = sSaveFilePath
xmlDOM.documentElement.appendChild (ndSaveFilePath)
Set ndFileData = xmlDOM.createElement("FileData")
ndFileData.dataType = "bin.base64"
adoStream.Type = 1
adoStream.Open
adoStream.LoadFromFile sFilePath
ndFileData.nodeTypedValue = adoStream.Read(-1)
adoStream.Close
xmlDOM.documentElement.appendChild (ndFileData)
Set objHTTP = CreateObject("Microsoft.XMLHTTP")
objHTTP.Open "POST", "http://localhost/up.asp", False
objHTTP.send xmlDOM.xml
Set responseDoc = CreateObject("MSXML2.DOMDocument")
responseDoc.loadXML (objHTTP.responseText) 'why not use objHTTP.ResponseXML,Who can tell me . liuruhong@263.net
sStatus = CInt(responseDoc.selectSingleNode("Response/Status").Text)
sResponseInfo = responseDoc.selectSingleNode("Response/Description").Text
End Sub
end class
sub UploadFile
set objUPF=new CFileUpload
objUPF.FilePath =file_path.value
objUPF.FileName =file_name.value
objUPF.SaveFilePath =save_path.value
objUPF.UploadFile
div_message.innerText = objUPF.ResponseInformation
end sub
//-->
</script>
<HTML>
<HEAD><TITLE>文件上传</TITLE></HEAD>
<BODY>
文件名:
<input id="file_name" type="text"><br>
保存路径:<input id="save_path" type="text"><br>
文件:
<input id="file_path" type="file" value="选择文件" onchange='file_name.value=mid(file_path.value,instrrev(file_path.value ,"\")+1)'>
<br>
<INPUT id=btn_send name="btn_send" type=button value="FILE SEND" onclick="UploadFile">
<DIV id=div_message>Ready</DIV>
</BODY>
</HTML>
<SCRIPT LANGUAGE=javascript>
<!--
/*
下面函数是JavaScript的实现方式,采用OOP的方式实现
可能有部分问题,如果哪个兄弟有空帮我调试一下
*/
/*
function CFileUpload(sFilePath,sFileName,sSaveToPath,sUploadServer){
this.FilePath=sFilePath;
this.FileName=sFileName;
this.UploadServer=sUploadSerer;
this.SaveFileToPath=sSaveToPath;
alert('liu');
}
CFileUpload.prototype.Upload = function(){
var adoStream=new ActiveXObject("ADODB.Stream"); //文件流对象
var xmlDOM =new ActiveXObject("MSXML2.DOMDocument"); //XML对象
xmlDOM.loadXML('<?xml version="1.0" ?> <root/>');
xmlDOM.documentElement.setAttribute("xmlns:dt","urn:schemas-microsoft-com:datatypes");
var ndFileData=xmlDOM.createElement("FileData")
ndFileData.dataType="bin.base64";
adoStream.Type =1 ;//设置为二进制类型
adoStream.Open();
adoStream.LoadFromFile(this.FilePath);
ndFileData.nodeTypedValue=adoStream.Read(-1);
ado_stream.Close();
xmlDOM.appendChild(ndFileData);
//添加文件名
var ndFileName=xmlDOM.createElement("FileName");
ndFileName.value=this.FileName;
xmlDOM.appendChild(ndFileName);
//添加保存文件路径
var ndSaveToPath=xmlDOM.createElement("SaveFilePath");
ndSaveToPath.value=this.SaveFileToPath;
xmlDOM.appendChild(ndSaveToPath);
/*
var objHTTP=new ActiveXObject("Microsoft.XMLHTTP");
objHTTP.open("POST","UploadService.asp",false);
alert("hello");
}
function UploadFile(){
var upf=new CFileUpload();
upf.FilePath=file_path.value ;
upf.FileName=file_name.value ;
upf.SaveToFilePath=null;
alert("kk");
upf.Uplad();
}
*/
//-->
</SCRIPT>
有些东西写的不完善,这段日子比较忙,有哪个大哥有空帮我写完吧
我的E-Mail:liuruhong@263.net ,QQ:17886612,欢迎交流