2007年1月20日星期六

web服务器接收WPS http上传文件

最近在为MTK的WPS的http上传文件方式实现web服务器端接受方式,得到临时代码如下,请多指正。
 
 
 
原理:
------------------------------------------------
获得WPS上传的数据流处理后的字符串如下:

--NAWG.MultipartBoundary.334992
content-disposition: form-data

<?xml version='1.0' encoding='UTF-8'?><user><userName>nameabc</userName><userPwd>pwdddabc</userPwd></user>
--NAWG.MultipartBoundary.334992--

 
其中
<?xml version='1.0' encoding='UTF-8'?><user><userName>nameabc</userName><userPwd>pwdddabc</userPwd></user>
是上传的文件内容。
 
 
demo:
------------------------------------------------
<!--#include file="WPSUploadXmlFile.asp"-->
<%
dim wps
wps = new WPSUploadXmlFile
response.write(wps.toString())
response.write(wps.getNodeValue("node", 0))
%>
code (WPSUploadXmlFile.asp):
------------------------------------------------

<%
' http://www.xianyun.org
' hotoo.cn@gmail.com
' 17/1/2007

class WPSUploadXmlFile
    ' |============== defind ====================|
    private isDebug
    private maxSize
    private size ' total size of client upload.
    private binFile ' the binary data from client.
    private binXml ' the xml binary data of binFileData, between chrB(60)("<") and chrB(62)(">")
    private sXml ' the xml string from client.
    private oStream
    private oXmlDOM
    private uploadPath ' upload file base path.
   
    ' |============ public function ===============|
    public function getSize()
        getSize = size
    end function
   
    public function setIsDebug(bIsDebug)
        isDebug = bIsDebug
    end function
   
    public function setMaxSize(iMaxSize)
        maxSize = iMaxSize
    end function
   
    public sub setUploadPath(path)
        uploadPath = path
    end sub
   
    public function getXmlString()
        getXmlString = sXml ' getXmlstr(bin2str(binFile))
    end function
   
    public function toString()
        toString = getXmlString()
    end function
   
    public function getNodeValue(nodeName, index)
        if isEmpty(oXmlDOM) then call initXmlDOM()
        on error resume next
        getNodeValue = oXmlDOM.getElementsByTagName (nodeName).item(index).text
        if err.number <> 0 then call printError("parse node " & nadeName & "failed at index " & index & ".")
    end function
   
    public function getNodeTypedValue(nodeName, index)
        if isEmpty (oXmlDOM) then call initXmlDOM()
        on error resume next
        'if Lcase(getNodeAttribute(nodeName, index, "dt:dt")) = " bin.base64" then
        getNodeTypedValue = oXmlDOM.getElementsByTagName(nodeName).item(index).nodeTypedValue
        if err.number <> 0 then call printError("parse node " & nadeName & "failed at index " & index & ".")
    end function
 
    public function getNodeAttribute(nodeName, index, attrName)
        if isEmpty(oXmlDOM) then call initXmlDOM()
        on error resume next
        getNodeAttribute = oXmlDOM.getElementsByTagName(nodeName).item(index).getAttribute(attrName)
        if err.number <> 0 then call printError("parse attribute " & attrName & " failed in node " & nodeName & " at index " & index & ".")
    end function
   
    public function saveToFile(nodeName, index)
        dim fileName
        dim counter : counter = 0
        if isEmpty(oStream) then set oStream = createStream()
        do
            err.clear()
            on error resume next
            fileName = getAutoFileName() & "." & getNodeAttribute(nodeName, index, "postfix")
            oStream.write getNodeTypedValue(nodeName, index)
            oStream.saveToFile server.mapPath(fileName), 1 ' 1=adSaveCreateNotExist, 2=adSaveCreateOverWrite
            if err.number = 0 then exit do
            counter = counter + 1
        loop while counter < 3
        if err.number <> 0 then printError("save to file error:" & err.description)
        saveToFile = fileName
    end function
   
    ' |============= private =======================|
   
    private sub class_initialize
        isDebug = false
        maxSize = 0
        uploadPath = ""
        call initData()
    end sub

    private sub class_terminate
        on error resume next
        set sXml = nothing
        if not isEmpty(oStream) then
            oStream.close()
            set oStream = nothing
        end if
        if not isEmpty(oXmlDOM) then
            set oXmlDOM = nothing
        end if
        if err.number <> 0 then printError("destroy error:" & err.description)
    end sub
   
    private function createStream()
        on error resume next
        set createStream = Server.CreateObject("ADODB.Stream ")
        createStream.type = 1 ' 1=adTypeBinary
        createStream.open ()
        if err.number <> 0 then call printError("create stream object failed.")
    end function
   
    private function createXmlDOM()
        on error resume next
        set createXmlDOM = Server.CreateObject("msxml2.DOMDocument")
        createXmlDOM.async = false
        if err.number <> 0 then call printError("create xml dom failed.")
    end function
   
    private sub initXmlDOM()
        if not isEmpty(oXmlDOM) then exit sub
        set oXmlDOM = createXmlDOM()
        oXmlDOM.loadXml(sXml)
        if oXmlDOM.parseError.errorCode <> 0 then
            printError("parse xml data failed." &_
                "line:" & oXmlDOM.parseError.line & "," &_
                "description:" & oXmlDOM.parseError.reason)
        end if
    end sub
   
    private function bin2str(bindata)
        dim olen,olow,return,skip,i
        skip=0
        return = ""
        if not isnull(bindata) then
            olen = lenb(bindata)
            for i = 1 to olen
                if skip = 0 then
                    olow = midb(bindata, i, 1)
                    if ascb(olow) > 127 then
                        return = return & chr(ascw(midb(bindata,i+1,1) & olow))
                        skip = 1
                    else
                        return = return & chr(ascb(olow))
                    end if
                else
                    skip = 0
                end if
            next
        end if
        bin2str = return
    end function

    private function str2bin(strdata)
        dim ochar,oasc,olow,ohigh,return,i
        return = ""
        for i = 1 to len(strdata)
            ochar = mid(strdata,i,1)
            oasc = asc(ochar)
            if oasc < 0 then
                oasc = oasc + 65535
            end if
            if oasc > 255 then
                olow = left(hex(asc(ochar)),2)
                ohigh = right(hex(asc(ochar)),2)
                return = return & chrb("&h" & olow) & chrb("&h" & ohigh)
            else
                return = return & chrb(ascb(ochar))
            end if
        next
        str2bin = return
    end function
   
    private function getAutoFileName()
        dim ranNum
        dim dtNow
        dtNow = now()
        randomize
        ranNum = int(90000 * rnd) + 10000
        getAutoFileName = year(dtNow) & right("0" & month(dtNow),2) & right("0" & day(dtNow),2) & right("0" & hour(dtNow),2) & right("0" & minute(dtNow),2) & right("0" &   second(dtNow),2) & ranNum
    end function

    ' ! ----------------- untapped. ----------------------------
    ' return binary data of xml file.
    private function getBinXmlData(filedata)
        dim bCrLf, iStart, iEnd
        iStart = inStrB(1, fileData, chrB(60))
        iEnd = inStrB(iStart, fileData, chrB(62)) + 1
        if iEnd > iStart and iEnd > 0 then
            xmlData = midB(fileData, iStart, iEnd)
        else
            xmlData = "xml data failed."
        end if
        getXmlData = xmlData
    end Function
   
    ' return string between frist char "<" and last char ">"
    private function getXmlStr(str)
        dim iStart, iLen, xmlData
        iStart = inStr(1, str, chr(60))
        iLen = InStrRev(str, chr(62), Len(str), 0) + 1 - iStart
        if iLen > 0 and iStart > 0 then
            xmlData = mid(str, iStart, iLen)
        else
            xmlData = "xml data failed."
        end if
        getXmlStr = xmlData
    end function
   
    private function initData()
        size = request.totalBytes
        binFile = request.binaryRead(size)
        sXml = getXmlStr(bin2str(binFile))
    end function
   
    private sub printError(sErr)
        err.clear()
        if not isDebug then exit sub
        response.write (sErr)
        response.end()
    end sub
end class

%>

没有评论:

发表评论