[求助] 用 IE 上传文件就差这一步了,报错(Required MultipartFile parameter 'file' is not present)

其他问题 小茨求学 ⋅ 于 2019-12-28 17:37:54 ⋅ 2165 阅读

用下述代码,生成参数,发送请求头后,返回:{"code":501,"msg":"Required MultipartFile parameter 'file' is not present"},求指导。

模拟提报代码:
sub Main()
Dim FileName As String
FileName = "C:\Users\danl.txt"
With CreateObject("winhttp.winhttprequest.5.1")
.Open "POST", "https://datasource/localConnection/uploadFile", False
.setRequestHeader "Content-Type", "multipart/form-data; boundary=---------------------------1997060815188" '要与下面SendBody里的一致
.setRequestHeader "Accept-Encoding", "gzip, deflate, br"
.setRequestHeader "Cookie", MyCookie
.setRequestHeader "Connection", "Keep-Alive"
.send SendBody(FileName) '发送字串
Debug.Print .responseText '可以登录论坛到发帖页面查看是否有“未使用附件”
End With
end sub

参数生成代码:
Function SendBody(ByVal Fn As String)
Dim MyStr As String
Dim b() As Byte
Dim StrByte() As Byte
Dim sLen As Long

MyStr = "-----------------------------1997060815188" '系统产生的随机数字   '头部
MyStr = MyStr & vbCrLf & "Content-Disposition: form-data; name=""config""" & vbCrLf & vbCrLf & "%7B%22fileType%22%3A0%2C%22delimiter%22%3A0%7D"
MyStr = MyStr & vbCrLf & "-----------------------------1997060815188" '系统产生的随机数字   '头部
MyStr = MyStr & vbCrLf & "Content-Disposition: form-data; name=""file""; filename=""danl.csv"""
MyStr = MyStr & vbCrLf & "Content-Type: application/vnd.ms-excel" & vbCrLf
MyStr = MyStr & vbCrLf

' MsgBox MyStr
StrByte = StrConv(MyStr, vbFromUnicode128) '将之前的字符串转为byte

b = FileToByte(Fn) '将要上传的文件转为byte
'    合并字符串及文件成一个byte

Debug.Print b
sLen = UBound(StrByte)
ReDim Preserve StrByte(sLen + UBound(b) - 1)
For i = 2 To UBound(b)
    StrByte(sLen + i - 1) = b(i)
Next

MyStr = vbCrLf & "-----------------------------1997060815188--"  '结束多2杆
MyStr = MyStr & vbCrLf

' MsgBox MyStr
b = StrConv(MyStr, vbFromUnicode128)
' Debug.Print b

sLen = UBound(StrByte)
ReDim Preserve StrByte(sLen + UBound(b) + 1)
For i = 0 To UBound(b)
    StrByte(sLen + i + 1) = b(i)
Next

SendBody = StrByte
Debug.Print SendBody

End Function
Function FileToByte(ByVal Fn As String)

 With CreateObject("adodb.stream")
    .Open
    .LoadFromFile Fn
    .Charset = "GB2312"
    .Type = 1
    FileToByte = .Read
    .Close
End With

End Function

回复数量: 0
    暂无评论~~
    • 请务必阅读并严格遵守《社区管理规范与使用说明》
    • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`, 更多语法请见这里 Markdown 语法
    • 支持表情,使用方法请见 发送表情,可用的 Emoji 见 :metal: :point_right: Emoji 列表 :star: :sparkles:
    • 上传图片, 支持拖拽和剪切板粘贴上传, 格式限制 - jpg, png, gif
    • 不支持上传附件,请尽可能用文字和图片将问题描述清楚,如实在需要上传附件,可上传到 共享网盘 后分享链接
    • 发布框支持本地存储功能,会在内容变更时保存,「提交」按钮点击时清空
      请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
    Ctrl+Enter