久久久久中文字幕精品无码免费,欧美成www,亚洲天色香,国产精品-色哟哟

您現(xiàn)在的位置: 中國農(nóng)業(yè)人才網(wǎng) >> 職場資訊 >> 黑板報 >> 正文

在ASP中模擬form上傳文件(以multipart/form-data編碼)

2012-3-31 本站原創(chuàng)
<%
Public Const adTypeBinary = 1
Public Const adTypeText = 2
Public Const adLongVarBinary = 205

'字節(jié)數(shù)組轉(zhuǎn)指定字符集的字符串
Public Function BytesToString(vtData, ByVal strCharset)
    Dim objFile
    Set objFile = Server.CreateObject("ADODB.Stream")
    objFile.Type = adTypeBinary
    objFile.Open
    If VarType(vtData) = vbString Then
        objFile.Write BinaryToBytes(vtData)
    Else
        objFile.Write vtData
    End If
    objFile.Position = 0
    objFile.Type = adTypeText
    objFile.Charset = strCharset
    BytesToString = objFile.ReadText(-1)
    objFile.Close
    Set objFile = Nothing
End Function

'字節(jié)字符串轉(zhuǎn)字節(jié)數(shù)組,即經(jīng)過MidB/LeftB/RightB/ChrB等處理過的字符串
Public Function BinaryToBytes(vtData)
    Dim rs
    Dim lSize
    lSize = LenB(vtData)
    Set rs = Server.CreateObject("ADODB.RecordSet")
    rs.Fields.Append "Content", adLongVarBinary, lSize
    rs.Open
    rs.AddNew
    rs("Content").AppendChunk vtData
    rs.Update
    BinaryToBytes = rs("Content").GetChunk(lSize)
    rs.Close
    Set rs = Nothing
End Function

'指定字符集的字符串轉(zhuǎn)字節(jié)數(shù)組
Public Function StringToBytes(ByVal strData, ByVal strCharset)
    Dim objFile
    Set objFile = Server.CreateObject("ADODB.Stream")
    objFile.Type = adTypeText
    objFile.Charset = strCharset
    objFile.Open
    objFile.WriteText strData
    objFile.Position = 0
    objFile.Type = adTypeBinary
    If UCase(strCharset) = "UNICODE" Then
        objFile.Position = 2 'delete UNICODE BOM
    ElseIf UCase(strCharset) = "UTF-8" Then
        objFile.Position = 3 'delete UTF-8 BOM
    End If
    StringToBytes = objFile.Read(-1)
    objFile.Close
    Set objFile = Nothing
End Function

'獲取文件內(nèi)容的字節(jié)數(shù)組
Public Function GetFileBinary(ByVal strPath)
    Dim objFile
    Set objFile = Server.CreateObject("ADODB.Stream")
    objFile.Type = adTypeBinary
    objFile.Open
    objFile.LoadFromFile strPath
    GetFileBinary = objFile.Read(-1)
    objFile.Close
    Set objFile = Nothing
End Function

'XML Upload Class
Class XMLUploadImpl
Private xmlHttp
Private objTemp
Private strCharset, strBoundary

Private Sub Class_Initialize()
    Set xmlHttp = Server.CreateObject("MSXML2.ServerXMLHTTP")
    Set objTemp = Server.CreateObject("ADODB.Stream")
    objTemp.Type = adTypeBinary
    objTemp.Open
    strCharset = "GBK"
    strBoundary = GetBoundary()
End Sub

Private Sub Class_Terminate()
    objTemp.Close
    Set objTemp = Nothing
    Set xmlHttp = Nothing
End Sub

'獲取自定義的表單數(shù)據(jù)分界線
Private Function GetBoundary()
    Dim ret(24)
    Dim table
    Dim i
    table = "ABCDEFGHIJKLMNOPQRSTUVWXZYabcdefghijklmnopqrstuvwxzy0123456789"
    Randomize
    For i = 0 To UBound(ret)
        ret(i) = Mid(table, Int(Rnd() * Len(table) + 1), 1)
    Next
    GetBoundary = "__NextPart__ " & Join(ret, Empty)
End Function 

'設(shè)置上傳使用的字符集
Public Property Let Charset(ByVal strValue)
    strCharset = strValue
End Property

'添加文本域的名稱和值
Public Sub AddForm(ByVal strName, ByVal strValue)
    Dim tmp
    tmp = "\r\n--$1\r\nContent-Disposition: form-data; name=""$2""\r\n\r\n$3"
    tmp = Replace(tmp, "\r\n", vbCrLf)
    tmp = Replace(tmp, "$1", strBoundary)
    tmp = Replace(tmp, "$2", strName)
    tmp = Replace(tmp, "$3", strValue)
    objTemp.Write StringToBytes(tmp, strCharset)
End Sub

'設(shè)置文件域的名稱/文件名稱/文件MIME類型/文件路徑或文件字節(jié)數(shù)組
Public Sub AddFile(ByVal strName, ByVal strFileName, ByVal strFileType, vtValue)
    Dim tmp
    tmp = "\r\n--$1\r\nContent-Disposition: form-data; name=""$2""; filename=""$3""\r\nContent-Type: $4\r\n\r\n"
    tmp = Replace(tmp, "\r\n", vbCrLf)
    tmp = Replace(tmp, "$1", strBoundary)
    tmp = Replace(tmp, "$2", strName)
    tmp = Replace(tmp, "$3", strFileName)
    tmp = Replace(tmp, "$4", strFileType)
    objTemp.Write StringToBytes(tmp, strCharset)
    If VarType(vtValue) = (vbByte Or vbArray) Then
        objTemp.Write vtValue
    Else
        objTemp.Write GetFileBinary(vtValue)
    End If
End Sub

'設(shè)置multipart/form-data結(jié)束標記
Private Sub AddEnd()
    Dim tmp
    'tmp = Replace("\r\n--$1--\r\n", "$1", strBoundary)
        tmp = "\r\n--$1--\r\n" 
        tmp = Replace(tmp, "\r\n", vbCrLf) 
        tmp = Replace(tmp, "$1", strBoundary)
    objTemp.Write StringToBytes(tmp, strCharset)
    objTemp.Position = 2
End Sub

'上傳到指定的URL,并返回服務(wù)器應(yīng)答
Public Function Upload(ByVal strURL)
    Call AddEnd
    xmlHttp.Open "POST", strURL, False
    xmlHttp.setRequestHeader "Content-Type", "multipart/form-data"
    xmlHttp.setRequestHeader "Content-Length", objTemp.size
    xmlHttp.Send objTemp
        If VarType(xmlHttp.responseBody) = (vbByte Or vbArray) Then 
            Upload = BytesToString(xmlHttp.responseBody, strCharset) 
        End If
End Function
End Class
%>
在包含該文件后用以下代碼調(diào)用
VB code
Dim UploadData
Set UploadData = New XMLUploadImpl
UploadData.Charset = "gb2312"
UploadData.AddForm "Test", "123456" '文本域的名稱和內(nèi)容
UploadData.AddFile "ImgFile", "F:\test.jpg", "image/jpg", GetFileBinary("F:\test.jpg")'圖片或者其它文件
Response.Write UploadData.Upload("http://localhost/receive.asp") 'receive.asp為接收頁面
Set UploadData = Nothing




本文地址:http://m.hxpgs.com.cn/news/n08/201203/6925.html,如要轉(zhuǎn)載,請注明轉(zhuǎn)載自中國農(nóng)業(yè)人才網(wǎng)
文章錄入:希望的田野    責任編輯:耕農(nóng) 
【字體: 】【打印此文】【關(guān)閉窗口
相關(guān)文章
    沒有相關(guān)文章
中國農(nóng)業(yè)人才網(wǎng)及旗下網(wǎng)站 版權(quán)所有 2003- © | 關(guān)于我們 | 聯(lián)系我們 | 服務(wù)條款 | 服務(wù)價格 | 付款方式 | 幫助中心 | 網(wǎng)站地圖

中國農(nóng)業(yè)人才網(wǎng)及旗下網(wǎng)站為您提供畜牧人才、種植人才、農(nóng)資人才、食品人才等求職招聘服務(wù)

国产无码精品二道本| 国产熟女专区| 精品久久久久中文慕人妻| tai9国产一区二区| 日本人妻系列中文字幕| wWW色天使COM| 六十路七十路超熟无码| 国产精品XXXX国产喷水| 激情图片五月丁香| 开心婷婷在线综合| 亚日韩综合一区二区| 乱人伦人妻精品一区二区| 永久人妻| 中字无码国产| 黄色视频aaabbb| 爆乳无码| 亚洲无码不卡视频| 亚洲噜噜中文| 亚洲系列欧美| 色综合五月网| 欧美性稚交6-12| 亚洲色图一二三区| HEYZO久久久| 亚洲A√一骑色欢网w| 亚洲色成人www在线观看| 天天免费视频插插插| 秋霞鲁丝片无码av| 日韩勉费视频| 四虎影视免费播放| 日韩夜晚黄色网站| 国产av国片精品有毛| 欧美、日韩、亚洲| 久久在现一级片| 国产传媒 欧美日韩| 91免费观看视频| 欧美视频久久久| wuye亚洲| 日韩乱伦 片| AV无码在线一区| 久久久av电影亚洲| 国产的欧美的www|