Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use VBS to simulate POST upload Files

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how to use VBS to simulate POST uploading files. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The code is as follows:

'XML Upload Class

Class XMLUpload

Private xmlHttp

Private objTemp

Private adTypeBinary, adTypeText

Private strCharset, strBoundary

Private Sub Class_Initialize ()

AdTypeBinary = 1

AdTypeText = 2

Set xmlHttp = CreateObject ("Msxml2.XMLHTTP")

Set objTemp = CreateObject ("ADODB.Stream")

ObjTemp.Type = adTypeBinary

ObjTemp.Open

StrCharset = "utf-8"

StrBoundary = GetBoundary ()

End Sub

Private Sub Class_Terminate ()

ObjTemp.Close

Set objTemp = Nothing

Set xmlHttp = Nothing

End Sub

'string-to-byte array of the specified character set

Public Function StringToBytes (ByVal strData, ByVal strCharset)

Dim objFile

Set objFile = 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

'get a byte array of file contents

Private Function GetFileBinary (ByVal strPath)

Dim objFile

Set objFile = CreateObject ("ADODB.Stream")

ObjFile.Type = adTypeBinary

ObjFile.Open

ObjFile.LoadFromFile strPath

GetFileBinary = objFile.Read (- 1)

ObjFile.Close

Set objFile = Nothing

End Function

'get a custom form data dividing line

Private Function GetBoundary ()

Dim ret (12)

Dim table

Dim i

Table = "abcdefghijklmnopqrstuvwxzy0123456789"

Randomize

For I = 0 To UBound (ret)

Ret (I) = Mid (table, Int (Rnd () * Len (table) + 1), 1)

Next

GetBoundary = "-" & Join (ret, Empty)

End Function

'set the character set used for uploading

Public Property Let Charset (ByVal strValue)

StrCharset = strValue

End Property

'add the name and value of the text field

Public Sub AddForm (ByVal strName, ByVal strValue)

Dim tmp

Tmp = "\ r\ nMushroom 1\ r\ nContent-Disposition: form-data; name="$2"\ r\ n\ r\ nFirst3"

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

'set the name of the file field / file name / file MIME type / file path or file byte array

Public Sub AddFile (ByVal strName, ByVal strFileName, ByVal strFileType, ByVal strFilePath)

Dim tmp

Tmp = "\ r\ nMushroom 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)

ObjTemp.Write GetFileBinary (strFilePath)

End Sub

'set the multipart/form-data end tag

Private Sub AddEnd ()

Dim tmp

Tmp = "\ r\ nmurmur1Mutual -\ r\ n"

Tmp = Replace (tmp, "\ r\ n", vbCrLf)

Tmp = Replace (tmp, "$1", strBoundary)

ObjTemp.Write StringToBytes (tmp, strCharset)

ObjTemp.Position = 2

End Sub

'upload to the specified URL and return the response to the server

Public Function Upload (ByVal strURL)

Call AddEnd

XmlHttp.Open "POST", strURL, False

XmlHttp.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & strBoundary

'xmlHttp.setRequestHeader "Content-Length", objTemp.size

XmlHttp.Send objTemp

Upload = xmlHttp.responseText

End Function

End Class

Dim UploadData

Set UploadData = New XMLUpload

UploadData.Charset = "utf-8"

Name and content of UploadData.AddForm "content", "Hello world" 'text field

UploadData.AddFile "file", "test.jpg", "image/jpg", "test.jpg"

WScript.Echo UploadData.Upload ("http://example.com/takeupload.php")"

Set UploadData = Nothing

Thank you for reading! This is the end of this article on "how to use VBS to simulate POST upload files". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report