ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 707|回复: 3

[求助] 通过VBA上传图片响应“文件不能为空”

[复制链接]

TA的精华主题

TA的得分主题

发表于 2023-7-19 13:00 | 显示全部楼层 |阅读模式
本帖最后由 ludexin 于 2023-7-22 01:06 编辑

需要将本地的文本上传到网站,但提交后响应的结果是{"success":0,"code":"","msg":"文件不能为空"},请帮忙看下是哪里有问题,谢谢!
  1. Option Explicit
  2. Dim username As String, password As String, orgCode As String, orgName As String, Distributor As String, Boundary As String, token As String

  3. Sub 图片上传()
  4.     Dim http As Object, url As String, filePath As String, base64Data As String, requestBody As String
  5.     Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
  6.     '请求网址
  7.     url = "https://example.com"
  8.     ' 图片转换Base64
  9.     filePath = "D:\1.jpg"
  10.     base64Data = JpgToBase64(filePath)
  11.     ' 创建请求体(body)
  12.     Boundary = "----WebKitFormBoundaryvBoSGERTQfZs7f8d"
  13.     requestBody = "--" & Boundary & vbCrLf
  14.     requestBody = requestBody & "Content-Disposition: form-data; name=""imageFile""; filename=""" & FilePathToFileName(filePath) & """" & vbCrLf
  15.     requestBody = requestBody & "Content-Type: image/jpeg" & vbCrLf & vbCrLf
  16.     requestBody = requestBody & base64Data & vbCrLf
  17.     requestBody = requestBody & "--" & Boundary & "--" & vbCrLf
  18.     ' 发送POST请求
  19.     Distributor = ""
  20.     token = ""
  21.     http.Open "POST", url, False
  22.     http.setRequestHeader "Distributor", Distributor 'Distributor必填
  23.     http.setRequestHeader "Authorization", "Bearer " & token 'Authorization必填
  24.     http.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & Boundary 'Content-Typen必填
  25.     http.Send requestBody
  26.     Debug.Print http.responsetext
  27.     ' 检查HTTP响应的状态码
  28.     If http.Status = 200 Then
  29.         MsgBox "上传成功!"
  30.     Else
  31.         MsgBox "上传失败: " & http.Status & " - " & http.responsetext
  32.     End If
  33.     ' 清理对象
  34.     Set http = Nothing
  35. End Sub

  36. Function FilePathToFileName(filePath As String) As String
  37.     Dim fileName As String
  38.     fileName = Mid(filePath, InStrRev(filePath, "") + 1)
  39.     FilePathToFileName = fileName
  40. End Function

  41. Function JpgToBase64(filePath As String) As String
  42.     Dim imageFile As Object
  43.     Set imageFile = CreateObject("ADODB.Stream")
  44.     With imageFile
  45.         .Type = 1 '二进制模式
  46.         .Open
  47.         .LoadFromFile filePath
  48.         Dim byteArray() As Byte
  49.         byteArray = .Read
  50.         JpgToBase64 = EncodeBase64(byteArray)
  51.         .Close
  52.     End With
  53.     Set imageFile = Nothing
  54. End Function

  55. Function EncodeBase64(ByRef inData() As Byte) As String
  56.     Dim xmlObj As Object
  57.     Set xmlObj = CreateObject("MSXML2.DOMDocument")
  58.     Dim elemNode As Object
  59.     Set elemNode = xmlObj.createElement("tmp")
  60.     With elemNode
  61.         .DataType = "bin.base64"
  62.         .nodeTypedValue = inData
  63.         EncodeBase64 = Replace(.text, vbLf, "")
  64.     End With
  65.     Set elemNode = Nothing
  66.     Set xmlObj = Nothing
  67. End Function
复制代码

11.jpg
12.jpg
13.jpg
14.jpg
15.jpg
11.jpg

TA的精华主题

TA的得分主题

发表于 2023-7-19 19:45 | 显示全部楼层
长传的不是 图片的 Base64,而是二进制 内容
看看 这个
https://club.excelhome.net/threa ... tml?_dsign=35e4d643
有偿 可以找我

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-7-19 22:12 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
我把上传文件修改为二进制流,提交报403.
  1. Option Explicit
  2. Dim username As String, password As String, orgCode As String, orgName As String, Distributor As String, Boundary As String, token As String

  3. Sub 图片上传()
  4.     Dim http As Object, url As String, filePath As String, base64Data As String, requestBody As String
  5.     Set http = CreateObject("WinHttp.WinHttpRequest.5.1")
  6.     '请求网址
  7.     url = "https://example.com"
  8.     ' 图片转换Base64
  9.     filePath = "D:\1.jpg"
  10.     fileData = FileToByte(filePath)
  11.     ' 创建请求体(body)
  12.     Boundary = "----WebKitFormBoundaryvBoSGERTQfZs7f8d"
  13.     requestBody = "--" & Boundary & vbCrLf
  14.     requestBody = requestBody & "Content-Disposition: form-data; name=""imageFile""; filename=""" & FilePathToFileName(filePath) & """" & vbCrLf
  15.     requestBody = requestBody & "Content-Type: image/jpeg" & vbCrLf & vbCrLf
  16.     requestBody = requestBody & fileData & vbCrLf
  17.     requestBody = requestBody & "--" & Boundary & "--" & vbCrLf
  18.     ' 发送POST请求
  19.     Distributor = ""
  20.     token = ""
  21.     http.Open "POST", url, False
  22.     http.setRequestHeader "Distributor", Distributor 'Distributor必填
  23.     http.setRequestHeader "Authorization", "Bearer " & token 'Authorization必填
  24.     http.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & Boundary 'Content-Typen必填
  25.     http.Send requestBody
  26.     Debug.Print http.responsetext
  27.     ' 检查HTTP响应的状态码
  28.     If http.Status = 200 Then
  29.         MsgBox "上传成功!"
  30.     Else
  31.         MsgBox "上传失败: " & http.Status & " - " & http.responsetext
  32.     End If
  33.     ' 清理对象
  34.     Set http = Nothing
  35. End Sub

  36. Function FilePathToFileName(filePath As String) As String
  37.     Dim fileName As String
  38.     fileName = Mid(filePath, InStrRev(filePath, "") + 1)
  39.     FilePathToFileName = fileName
  40. End Function


  41. Function FileToByte(strFileName As String)
  42.     '文件转流
  43.      With CreateObject("Adodb.Stream")
  44.         .Open
  45.         .Type = 1 'adTypeBinary
  46.         .LoadFromFile strFileName
  47.         FileToByte = .Read
  48.         .Close
  49.     End With
  50. End Function
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-7-19 22:14 | 显示全部楼层
本帖最后由 ludexin 于 2023-7-22 01:05 编辑

--------------
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-17 04:35 , Processed in 0.031002 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表