当前位置:网站首页>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 
边栏推荐
- Low power multi-channel wfas1431 wireless data acquisition and transmission instrument operation process description
- 嵌入式开发:调试嵌入式软件的技巧
- Leetcode148 sort linked list (merge method applied to merge)
- Seata的部署与微服务集成
- When AI encounters life and health, Huawei cloud builds three bridges for them
- 请问数据库规范的文档吗 参考一下?
- 研发了 5 年的时序数据库,到底要解决什么问题?
- Embedded development: skills of debugging embedded software
- Install dexdump on win10 and remove the shell
- sqlDeveloper工具快速入门
猜你喜欢
![[dry goods] data structure and algorithm principle behind MySQL index](/img/80/d5ab431cd5112b1637ee07d5b59afa.png)
[dry goods] data structure and algorithm principle behind MySQL index

全校软硬件基础设施一站式监控 ,苏州大学以时序数据库替换 PostgreSQL

TransC知识表示模型

在检测分割中一些轻量级网络模型(自己学习的笔记分享)

VP video structured framework

Flask send_ Absolute path traversal caused by file function

Install dexdump on win10 and remove the shell

OpenCV中图像算术操作与逻辑操作

研发了 5 年的时序数据库,到底要解决什么问题?

1对1直播源码——1对1语音聊天源码
随机推荐
GOM登录器配置免费版生成图文教程
Some lightweight network models in detection and segmentation (share your own learning notes)
Canvas mesh wave animation JS special effect
【文件上传漏洞-06】分布式配置文件攻击实验—以upload-labs-4为例
JS creative range select drag and drop plug-ins
Tdengine helps Siemens' lightweight digital solution simicas simplify data processing process
GOM login configuration free version generate graphic tutorial
UDP多线程在线聊天
『BaGet』带你一分钟搭建自己的私有NuGet服务器
The development of smart home industry pays close attention to edge computing and applet container technology
[deep learning] fully connected network
[1.2. return and risk of investment]
One stop monitoring of the software and hardware infrastructure of the whole university, and Suzhou University replaces PostgreSQL with time series database
Kubernetes ---- pod configuration resource quota
Kubernetes----Pod配置资源配额
注解和反射
JS wave animation effect menu style
TDengine 助力西门子轻量级数字化解决方案 SIMICAS 简化数据处理流程
What is restful style and its four specific implementation forms
【1.2.投资的收益和风险】