当前位置:网站首页>VBA 上传图片
VBA 上传图片
2022-07-26 14:29:00 【渔滒】
VBA 上传图片
问题来源于帖子:VBA怎么样通过winHttp、xmlHttp post上传图片呢 求大佬指点
整个过程并没有什么加密,比较简单,就不做什么分析了,使用Fiddler抓包查看,可以很快的用python复现
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()
基本上都是照抄,最后可以获取在线的图片地址。这么难点在于如何使用VBA来上传。
1.因为没有轮子,所有multipart 包装需要自己处理。
2.请求体包含了二进制文件
一、
为了解决第一个问题,可以在vba中生成的二进制数据(即Byte数组)与python的对比,直到两个数据完全一样。
二、因为要提交字节数组,那么必须要使用【WinHttp.WinHttpRequest.5.1】对象。
提交部分的代码如下
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
这里我自己写了一个类,为了方便拼接二进制数据
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
更多内容可以加入我的星球
边栏推荐
- Iscc2021 lock problem solution
- [deep learning] fully connected network
- C# Winfrom 常用功能整合
- Prediction and value evaluation of technology fusion relationship based on multiple features
- 图神经网络Core数据集介绍
- Leetcode question type priority queue (TOPK question)
- Would you please tell me if there is a way for Flink SQL not to output update_ before?
- UE4 智能指针和弱指针
- ISCC2021 LOCKK题解
- TDengine 助力西门子轻量级数字化解决方案 SIMICAS 简化数据处理流程
猜你喜欢

MySQL-03 数据库操作

Job 7.25 sorting and searching

Use cpolar to build a commercial website (apply for website security certificate)

GOM login configuration free version generate graphic tutorial

【深度学习】全连接网络
![[deep learning] fully connected network](/img/88/43f80b6a28773f9c4d2c31a8cb206b.png)
[deep learning] fully connected network

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

如何评价测试质量?

Leetcode question type priority queue (TOPK question)

【1.2.投资的收益和风险】
随机推荐
低功耗多通道WFAS1431无线数据采集采发仪使用流程说明
保证接口数据安全的10种方案
UE4 智能指针和弱指针
mysql5.7通过文件zip方式安装-九五小庞
Multithreading - thread pool
[ostep] 04 virtualized CPU - process scheduling strategy
智能家居行业发展,密切关注边缘计算和小程序容器技术
Jzoffer51- reverse pairs in the array (merge sort solution)
My meeting of OA project
Arithmetic operation and logic operation of image in opencv
Plato farm is expected to further expand its ecosystem through elephant swap
Use of URL download resources
【干货】MySQL索引背后的数据结构及算法原理
.net6 encounter with the League of heroes - create a game assistant according to the official LCU API
注解和反射
融合多自然语言处理任务的中医辅助诊疗方案研究——以糖尿病为例
Research on Chinese medicine assisted diagnosis and treatment scheme integrating multiple natural language processing tasks -- taking diabetes as an example
Leetcode36 effective Sudoku
Would you please tell me if there is a way for Flink SQL not to output update_ before?
Embedded development: skills of debugging embedded software