日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

vbs模拟post请求上传文件

發布時間:2023/12/18 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 vbs模拟post请求上传文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

''' VBS文件上傳類,二進制方式上傳
Class vbsFileUpload
? ? Public c_strDestURL ? ? ?' 文件上傳URL http://127.0.0.1:8080/AirportPro/widetable/uploadFile
? ? Public c_strFileName ? ? ' 要上傳的本地文件名
? ? Public c_strFieldName ? ?' 字段名,類似HTML表單Form中的input name
? ? Public c_strBoundary ? ? ' 文件上傳Post數據包中的分隔符
? ? Public c_strContentType ?' text/plain or image/pjpeg and so on "application/upload"
? ? Public c_strResponseText ' 文件上傳后,服務器返回的信息
? ? Public c_boolPrepared ? ?'
? ? Public c_strErrMsg ? ? ? ' 可能的錯誤信息

? ? Public Sub Class_Initialize()
? ? ? ? c_strDestURL ? ? = "http://127.0.0.1:8080/AirportPro/widetable/uploadFile"
? ? ? ? c_strFileName ? ?= "E:\1tyd\tyd.xls"
? ? ? ? c_strContentType = "application/upload"
? ? ? ? c_strFieldName ? = "file"
? ? ? ? c_strBoundary ? ?= "---------------------------7da1c52160186"
? ? ? ? c_boolPrepared ? = false
? ? End Sub
? ?
? ? Public Sub Class_Terminate
? ? End Sub
? ?
? ? ''' 公共調用函數,文件上傳
? ? Public Function vbsUpload
? ? ? ? CheckRequirements()
? ? ? ? If ?c_boolPrepared Then
? ? ? ? ? ? UploadFile c_strDestURL, c_strFileName, c_strFieldName
? ? ? ? Else
? ? ? ? ? ? 'WScript.Echo c_strErrMsg
? ? ? ? End If
? ? End Function
? ?
? ? ''' 檢查程序工作環境
? ? Private Function CheckRequirements
? ? ? ? Dim objFSO
? ? ? ? Set objFSO = CreateObject("Scripting.FileSystemObject")
? ? ? ? If Not objFSO.FileExists(c_strFileName) Then
? ? ? ? ? ? c_strErrMsg = c_strErrMsg & vbCrLf & "wen jian bu cun zai.."
? ? ? ? Else
? ? ? ? ? ? On Error Resume Next
? ? ? ? ? ? ? CreateObject "MSXML2.XMLHTTP"
? ? ? ? ? ? ? If Not Err = 0 Then
? ? ? ? ? ? ? ? ? c_strErrMsg = c_strErrMsg & vbCrLf & Err.Descriptiof
? ? ? ? ? ? ? Else
? ? ? ? ? ? ? ? ? c_boolPrepared = True
? ? ? ? ? ? ? End If
? ? ? ? ? End If ? ? ??
? ? End Function
? ?
? ?
? ? ''' 文件上傳
? ? Private Function UploadFile(DestURL, FileName, FieldName)
? ? ? ? Dim FileContents, FormData,Boundary
? ? ? ? Boundary ? ? = c_strBoundary
? ? ? ? FileContents = GetFile(FileName) ' 二進制文件內容
? ? ? ? FormData ? ? = BuildFormData(FileContents, Boundary, FileName, FieldName)
? ? ? ? WinHTTPPostRequest DestURL, FormData, Boundary
? ? End Function
? ?
? ? ''' WinHTTPPostRequest
? ? Private Function WinHTTPPostRequest(URL, FormData, Boundary)
? ? ? ? Dim xmlhttp
? ? ? ? ? Set xmlhttp = CreateObject("MSXML2.XMLHTTP")
? ? ? ? ? On Error Resume Next
? ? ? ? ? xmlhttp.Open "POST", URL, False
? ? ? ? ?xmlhttp.setRequestHeader "Content-Type", "multipart/form-data; boundary=" + Boundary
? ? ? ? ? xmlhttp.send FormData
? ? ? ? ? c_strResponseText = xmlhttp.responseText ' 服務端返回信息
? ? ? ? ? Set xmlhttp = Nothing
? ? End Function

? ? '''組合上傳數據包 multipart/form-data document Header + Content
? ? Private Function BuildFormData(FileContents, Boundary, FileName, FieldName)
? ? ? Dim FormData, Pre, Po, ContentType
? ? ? ContentType = c_strContentType
? ? ?
? ? ? 'The two parts around file contents In the multipart-form data.
? ? ? Pre = "--" + Boundary + vbCrLf + mpFields(FieldName, FileName, ContentType)
? ? ? Po = vbCrLf + "--" + Boundary + "--" + vbCrLf
? ? ?
? ? ? 'Build form data using recordset binary field
? ? ? Const adLongVarBinary = 205
? ? ? Dim RS: Set RS = CreateObject("ADODB.Recordset")
? ? ? RS.Fields.Append "b", adLongVarBinary, Len(Pre) + LenB(FileContents) + Len(Po)
? ? ? RS.Open
? ? ? RS.AddNew
? ? ? ? Dim LenData
? ? ? ? 'Convert Pre string value To a binary data
? ? ? ? LenData = Len(Pre)
? ? ? ? RS("b").AppendChunk (StringToMB(Pre) & ChrB(0))
? ? ? ? Pre = RS("b").GetChunk(LenData)
? ? ? ? RS("b") = ""
? ? ? ?
? ? ? ? 'Convert Po string value To a binary data
? ? ? ? LenData = Len(Po)
? ? ? ? RS("b").AppendChunk (StringToMB(Po) & ChrB(0))
? ? ? ? Po = RS("b").GetChunk(LenData)
? ? ? ? RS("b") = ""
? ? ? ?
? ? ? ? 'Join Pre + FileContents + Po binary data
? ? ? ? RS("b").AppendChunk (Pre)
? ? ? ? RS("b").AppendChunk (FileContents)
? ? ? ? RS("b").AppendChunk (Po)
? ? ? RS.Update
? ? ? FormData = RS("b")
? ? ? RS.Close
? ? ? BuildFormData = FormData
? ? End Function
? ?
? ? 'Converts OLE string To multibyte string
? ? Private Function StringToMB(S)
? ? ? Dim I, B
? ? ? For I = 1 To Len(S)
? ? ? ? B = B & ChrB(Asc(Mid(S, I, 1)))
? ? ? Next
? ? ? StringToMB = B
? ? End Function

? ?
? ? ''' 組織HTTP頭
? ? Private Function mpFields(FieldName, FileName, ContentType)
? ? ? Dim MPTemplate 'template For multipart header
? ? ? MPTemplate = "Content-Disposition: form-data; name=""{field}"";" + _
? ? ? ?" filename=""{file}""" + vbCrLf + _
? ? ? ?"Content-Type: {ct}" + vbCrLf + vbCrLf
? ? ? Dim Out
? ? ? Out = Replace(MPTemplate, "{field}", FieldName)
? ? ? Out = Replace(Out, "{file}", FileName)
? ? ? mpFields = Replace(Out, "{ct}", ContentType)
? ? End Function
? ?
? ? ''' 二進制載入文件內容
? ? Private Function GetFile(FileName)
? ? ? Dim Stream: Set Stream = CreateObject("ADODB.Stream")
? ? ? Stream.Type = 1 'Binary
? ? ? Stream.Open
? ? ? Stream.LoadFromFile FileName
? ? ? GetFile = Stream.Read
? ? ? Stream.Close
? ? End Function
End Class

?


Dim myUpload
Set myUpload = New vbsFileUpload
myUpload.c_strDestURL ? ? = "http://127.0.0.1:8080/AirportPro/widetable/uploadFile?" ?' 必選
myUpload.c_strFileName ? ?= "E:\1tyd\tyd.xls" ? ' 必選
myUpload.c_strFieldName ? = "file" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?' 必選
myUpload.c_strContentType = "application/upload" ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ' 可選
myUpload.vbsUpload()
'''WScript.Echo myUpload.c_strResponseText
'''WScript.Echo myUpload.c_strErrMsg
Set myUpload = Nothing

總結

以上是生活随笔為你收集整理的vbs模拟post请求上传文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。