当前位置:网站首页>VBA upload pictures
VBA upload pictures
2022-07-26 14:42:00 【Yuying】
VBA To upload pictures
The question comes from the post :VBA How to pass winHttp、xmlHttp post Upload pictures Ask the boss for advice
There is no encryption in the whole process , Relatively simple , Don't do any analysis , Use Fiddler Grab the bag and check , It can be used quickly python Reappear
import requests
from io import BytesIO
from requests_toolbelt.multipart.encoder import MultipartEncoder
def main():
file_name = '6da2ddc8124d0ba7045c38925eb857c0.jpeg'
fields = {
"image": (file_name, BytesIO(open(file_name, 'rb').read()), 'image/jpeg'),
"fileId": file_name,
"initialPreview": '[]',
"initialPreviewConfig": '[]',
"initialPreviewThumbTags": '[]'
}
multipart = MultipartEncoder(
fields=fields,
boundary='----WebKitFormBoundaryAx0QwuMECh2eIreV'
)
headers = {
"X-Requested-With": "XMLHttpRequest",
"Content-Type": multipart.content_type,
}
data = multipart.to_string()
response = requests.post('https://www.imgtp.com/upload/upload.html', headers=headers, data=data)
print(response.status_code)
print(response.headers)
print(response.request.headers)
print(response.json())
if __name__ == '__main__':
main()
Basically copy , Finally, you can get the online picture address . The difficulty lies in how to use VBA To upload .
1. Because there are no wheels , all multipart The packaging needs to be handled by yourself .
2. The request body contains binary files
One 、
To solve the first problem , Can be in vba Binary data generated in ( namely Byte Array ) And python Comparison of , Until the two data are exactly the same .
Two 、 Because we need to submit byte array , Then you have to use 【WinHttp.WinHttpRequest.5.1】 object .
The code of the submission part is as follows
Sub upload()
Const Boundary As String = "----WebKitFormBoundaryAx0QwuMECh2eIreV"
Const file_name As String = "6da2ddc8124d0ba7045c38925eb857c0.jpeg"
Dim image_byte() As Byte
Dim data() As Byte
image_byte = openbytefile(ActiveWorkbook.path & "\" + file_name)
Dim bytes As bytearray
Set bytes = New bytearray
bytes.update encodeutf8("--" & Boundary & vbNewLine)
bytes.update encodeutf8("Content-Disposition: form-data; name=""image""; filename=""" & file_name & """" & vbNewLine)
bytes.update encodeutf8("Content-Type: image/jpeg" & vbNewLine)
bytes.update encodeutf8(vbNewLine)
bytes.update image_byte
bytes.update encodeutf8(vbNewLine)
bytes.update encodeutf8("--" & Boundary & vbNewLine)
bytes.update encodeutf8("Content-Disposition: form-data; name=""fileId""" & vbNewLine)
bytes.update encodeutf8(vbNewLine & vbNewLine)
bytes.update encodeutf8(file_name & vbNewLine)
bytes.update encodeutf8("--" & Boundary & vbNewLine)
bytes.update encodeutf8("Content-Disposition: form-data; name=""initialPreview""" & vbNewLine)
bytes.update encodeutf8(vbNewLine & vbNewLine)
bytes.update encodeutf8("[]" & vbNewLine)
bytes.update encodeutf8("--" & Boundary & vbNewLine)
bytes.update encodeutf8("Content-Disposition: form-data; name=""initialPreviewConfig""" & vbNewLine)
bytes.update encodeutf8(vbNewLine & vbNewLine)
bytes.update encodeutf8("[]" & vbNewLine)
bytes.update encodeutf8("--" & Boundary & vbNewLine)
bytes.update encodeutf8("Content-Disposition: form-data; name=""initialPreviewThumbTags""" & vbNewLine)
bytes.update encodeutf8(vbNewLine & vbNewLine)
bytes.update encodeutf8("[]" & vbNewLine)
bytes.update encodeutf8("--" & Boundary & "--" & vbNewLine)
data = bytes.digest
Dim http As Object
Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
Dim htmlurl As String
htmlurl = "https://www.imgtp.com/upload/upload.html"
Dim htmltext As String
With http
.Open "POST", htmlurl, False
.SetRequestHeader "User-Agent", "python-requests/2.25.1"
.SetRequestHeader "Accept-Encoding", "gzip, deflate"
.SetRequestHeader "Accept", "*/*"
.SetRequestHeader "Connection", "keep-alive"
.SetRequestHeader "X-Requested-With", "XMLHttpRequest"
.SetRequestHeader "Content-Type", "multipart/form-data; boundary=" & Boundary
.SetRequestHeader "Content-Length", CStr(UBound(data) + 1)
.Send data
Dim body() As Byte
body = .responseBody
htmltext = encoding(body, "utf-8")
End With
Debug.Print htmltext
End Sub
Here I write a class myself , In order to facilitate the splicing of binary data
Option Explicit
Private data() As Byte
Public Sub Class_Initialize()
ReDim data(0 To 0)
End Sub
Public Sub update(body() As Byte)
ReDim Preserve data(0 To (UBound(data) - LBound(data) + UBound(body) - LBound(body) + 1))
Dim i As Long
For i = UBound(body) To 0 Step -1
data(UBound(data) - i) = body(UBound(body) - i)
Next i
End Sub
Public Property Get digest() As Byte()
Dim temp() As Byte
ReDim temp(0 To UBound(data) - 1)
Dim i As Long
For i = 1 To UBound(data) Step 1
temp(i - 1) = data(i)
Next i
digest = temp
End Property
Public Sub Class_Terminate()
ReDim data(0 To 0)
End Sub
More content can be added to my planet 
边栏推荐
- UDP多线程在线聊天
- C nanui related function integration
- C # use shift > > and operation and & to judge whether the two binary numbers have changed
- maya将模型导入到unity
- 【整数规划】
- Fill in the questionnaire and receive the prize | we sincerely invite you to fill in the Google play academy activity survey questionnaire
- C语言入门必刷100题合集之每日一题(1-20)
- Wechat applet - "do you really understand the use of applet components?
- C common function integration
- Realize the full link grayscale based on Apache APIs IX through MSE
猜你喜欢

Win11 running virtual machine crashed? Solution to crash of VMware virtual machine running in win11

目标跟踪相关知识总结

Seata deployment and microservice integration

llcc68广播唤醒简单说明

As the "first city" in Central China, Changsha's "talent attraction" has changed from competition to leadership

《MySQL高级篇》五、InnoDB数据存储结构

4 kinds of round head arrangement styles overlay styles

The development of smart home industry pays close attention to edge computing and applet container technology

中部“第一城”,长沙“人才引力”从争先到领先

14. Bridge based active domain adaptation for aspect term extraction reading notes
随机推荐
Devops system of "cloud native" kubesphere pluggable components
Keyboard shortcut to operate the computer (I won't encounter it myself)
Classic line style login interface
SiamFC:用于目标跟踪的全卷积孪生网络
Learning basic knowledge of Android security
手持振弦采集仪VH03各种接口使用说明
Fill in the questionnaire and receive the prize | we sincerely invite you to fill in the Google play academy activity survey questionnaire
CAS单点登录
『BaGet』带你一分钟搭建自己的私有NuGet服务器
SiamRPN:建议区域网络与孪生网络
c# 用移位 >> 和运算与 &判断两个 二进制数 是否发生过改变
Would you please refer to the document of Database specification?
What is the problem of the time series database that has been developed for 5 years?
The development of smart home industry pays close attention to edge computing and applet container technology
Annotation and reflection
Embedded development: skills of debugging embedded software
在检测分割中一些轻量级网络模型(自己学习的笔记分享)
如何评价测试质量?
Mysql-04 storage engine and data type
Tdengine helps Siemens' lightweight digital solution simicas simplify data processing process