我有一些代码,但我不明白为什么它不工作...
Sub UploadJPGWithCURL()
Dim winHttpReq As Object
Dim fileData As String
Dim boundary As String
Dim fileName As String
Dim filePath As String
filePath = "Z:\Desktop\testimage.jpg"
fileName = "testimage.jpg"
boundary = "----------------------------" & Format(Now, "ddmmyyyyhhmmss")
fileData = "--" & boundary & vbCrLf
fileData = fileData & "Content-Disposition: form-data; name=""fileToUpload""; filename=""" & fileName & """" & vbCrLf
fileData = fileData & "Content-Type: image/jpeg" & vbCrLf & vbCrLf
'fileData = fileData & filePath & vbCrLf
fileData = fileData & getBinaryFile(filePath) & vbCrLf
fileData = fileData & "--" & boundary & "--" & vbCrLf
Debug.Print fileData
Set winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
winHttpReq.Open "POST", "[THE URL TO MY WEBSERVER PHP PAGE THAT IS GOING TO PROCESS THE UPLOAD]", False
winHttpReq.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & boundary
winHttpReq.send (fileData)
If winHttpReq.Status = 200 Then
Debug.Print winHttpReq.ResponseText
Else
Debug.Print "Request failed with status code: " & winHttpReq.Status
End If
End Sub
Function getBinaryFile(filePath)
Dim binaryStream
Set binaryStream = CreateObject("ADODB.Stream")
binaryStream.Type = 1
binaryStream.Open
binaryStream.LoadFromFile filePath
getBinaryFile = binaryStream.Read
binaryStream.Close
End Function
在服务器PHP上,$_FILES数组给出了这个文件的错误代码3,看起来是部分上传......(如果在php代码的顶部打印$_FILES数组,我会得到这个错误代码......
Array
(
[fileToUpload] => Array
(
[name] => testimage.jpg
[full_path] => testimage.jpg
[type] =>
[tmp_name] =>
[error] => 3
[size] => 0
)
)
任何帮助或指导将不胜感激!
谢谢
克里斯
1条答案
按热度按时间nmpmafwu1#
您必须转换文件的二进制内容和发送的数据,如下所示:
发送数据时:
创建此函数:
并像这样使用它:
测试成功: