【问题标题】:Upload a Picture to file.io (HTTP Post) in VBA在 VBA 中将图片上传到 file.io (HTTP Post)
【发布时间】:2018-05-01 02:18:33
【问题描述】:

我正在尝试使用他们的 Api(https://www.file.io/#one,见下文)在 Excel 中使用 VBA 上传带有 https://file.io 的文件。

我找到了这个线程how to upload file to file.io and get link,但是,我不知道如何准确地将它从 C# 传输到 VBA。

File.io 上的语法是:

$ curl -F "file=@test.txt" https://file.io
{"success":true,"key":"2ojE41","link":"https://file.io/2ojE41","expiry":"14 days"}
$ curl https://file.io/2ojE41 
This is a test
$ curl https://file.io/2ojE41
{"success":false,"error":404,"message":"Not Found"}

我当前的代码如下:

Set objhttp = CreateObject("MSXML2.ServerXMLHTTP")
URL = "https://file.io"
objhttp.Open "post", URL, False
objhttp.setRequestHeader "Content-type", "application/json" 
objhttp.Send ("file=@C:/Users/me/Downloads/image.jpg")
Debug.Print objhttp.responsetext

我的回复文本说:

{"success":false,"error":400,"message":"Trouble uploading file"}

我什至不确定路径中的“@”或者是否通常使用标准文件夹等。非常感谢提前!感谢所有帮助。

【问题讨论】:

  • 欢迎您!好问题。我现在没有时间研究这个,但期待看到答案。与此同时,this answer 可能有用。另外maybe this 也可以帮助...

标签: vba post http-post


【解决方案1】:

使用 XmlHttp VBA 发布 multipart/form-data 的步骤

  1. 使用 Chrome/Firefox/Fiddler 观看 HTTP 请求。
  2. 首先手动上传文件并查看浏览器所做的所有请求和响应
    (尤其是 xhr,状态码为 200 的文档请求)
  3. 在 post 请求中传递 cookie、参数

在这种情况下我使用了chrome浏览器,下图显示了浏览器在请求中发送的参数。


 Sub UploadFilesUsingVBA()
     'this proc will upload below files to https://file.io/
          '  png, jpg, txt

        Dim fileFullPath As String
        fileFullPath = "C:\Users\santosh\Desktop\abcd.txt"

        POST_multipart_form_data fileFullPath
    End Sub

文件上传成功时的确认信息


Private Function GetGUID() As String
    ' Generate uuid version 4 using VBA
    GetGUID = WorksheetFunction.Concat(WorksheetFunction.Dec2Hex(WorksheetFunction.RandBetween(0, 4294967295#), 8), "-", WorksheetFunction.Dec2Hex(WorksheetFunction.RandBetween(0, 65535), 4), "-", WorksheetFunction.Dec2Hex(WorksheetFunction.RandBetween(16384, 20479), 4), "-", WorksheetFunction.Dec2Hex(WorksheetFunction.RandBetween(32768, 49151), 4), "-", WorksheetFunction.Dec2Hex(WorksheetFunction.RandBetween(0, 65535), 4), WorksheetFunction.Dec2Hex(WorksheetFunction.RandBetween(0, 4294967295#), 8))

End Function

Private Function GetFileSize(fileFullPath As String) As Long

    Dim lngFSize As Long, lngDSize As Long
    Dim oFO As Object, OFS As Object

    lngFSize = 0
    Set OFS = CreateObject("Scripting.FileSystemObject")

    If OFS.FileExists(fileFullPath) Then
        Set oFO = OFS.getFile(fileFullPath)
        GetFileSize = oFO.Size
    Else
        GetFileSize = 0
    End If

    Set oFO = Nothing
    Set OFS = Nothing
End Function



Private Function ReadBinary(strFilePath As String)
    Dim ado As Object, bytFile
    Set ado = CreateObject("ADODB.Stream")
    ado.Type = 1
    ado.Open
    ado.LoadFromFile strFilePath
    bytFile = ado.Read
    ado.Close

    ReadBinary = bytFile

    Set ado = Nothing
End Function


Private Function toArray(str)
    Dim ado As Object
     Set ado = CreateObject("ADODB.Stream")
     ado.Type = 2
     ado.Charset = "_autodetect"
     ado.Open
     ado.WriteText (str)
     ado.Position = 0
     ado.Type = 1
     toArray = ado.Read()
     Set ado = Nothing
End Function


Sub POST_multipart_form_data(filePath As String)

    Dim oFields As Object, ado As Object
    Dim sBoundary As String, sPayLoad As String, GUID As String
    Dim fileType As String, fileExtn As String, fileName As String
    Dim sName As Variant

    fileName = Right(filePath, Len(filePath) - InStrRev(filePath, "\"))
    fileExtn = Right(filePath, Len(fileName) - InStrRev(fileName, "."))

    Select Case fileExtn
     Case "png"
        fileType = "image/png"
     Case "jpg"
        fileType = "image/jpeg"
     Case "txt"
        fileType = "text/plain"
    End Select

    Set oFields = CreateObject("Scripting.Dictionary")
    With oFields
        .Add "qquuid", GetGUID
        .Add "qqtotalfilesize", GetFileSize(filePath)
    End With

    sBoundary = String(27, "-") & "7e234f1f1d0654"
    sPayLoad = ""
    For Each sName In oFields
        sPayLoad = sPayLoad & "--" & sBoundary & vbCrLf
        sPayLoad = sPayLoad & "Content-Disposition: form-data; name=""" & sName & """" & vbCrLf & vbCrLf
        sPayLoad = sPayLoad & oFields(sName) & vbCrLf
    Next

    sPayLoad = sPayLoad & "--" & sBoundary & vbCrLf
    sPayLoad = sPayLoad & "Content-Disposition: form-data; name=""file""; " & "filename=""" & fileName & """" & vbCrLf
    sPayLoad = sPayLoad & "Content-Type: " & fileType & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf



     sPayLoad = sPayLoad & "--" & sBoundary & "--"


      Set ado = CreateObject("ADODB.Stream")
      ado.Type = 1
      ado.Open
      ado.Write toArray(sPayLoad)
      ado.Write ReadBinary(filePath)
      ado.Position = 0

    With CreateObject("MSXML2.ServerXMLHTTP")
        .Open "POST", "https://file.io", False
        .SetRequestHeader "Content-Type", "multipart/form-data; boundary=" & sBoundary
        .Send (ado.Read())
        MsgBox .responseText
    End With

End Sub

有助于回答此问题的链接
1.https://stackoverflow.com/a/43266809/2227085
2.https://wqweto.wordpress.com/

【讨论】:

  • 你好,为什么当我点击file.io创建的链接时我的文件被删除了?
  • 如何获取文件 io 创建的链接?
  • 我的图片文件重新下载时不支持,不知道为什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-08
  • 2015-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多