简体   繁体   English

在 VBA 中使用 curl 上传图像

[英]Upload image using curl in VBA

I have registered in a website that supports API and I have tested the CURL command line and it works fine for me我已经在一个支持 API 的网站上注册,我已经测试了 CURL 命令行,它对我来说很好用

curl --location --request POST "https://api.imgbb.com/1/upload?key=APIKEY" --form "image=iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="

this is API information page https://api.imgbb.com/这是 API 信息页面https://api.imgbb.com/

I have no great knowledge of how to use excel VBA to send an equivalent command line?我不知道如何使用 excel VBA 发送等效的命令行? I am totally new to such stuff and I have searched a lot and can't get how things go on我对这些东西完全陌生,我搜索了很多东西,但不知道事情是怎么回事

I have found this link that may be helpful but it is in VB.NET Help with Imgur API and VB.NET - Image POST我发现这个链接可能会有帮助,但它在 VB.NET Help with Imgur API and VB.NET - Image POST

** Now I got the solution and I have developed it like that ** 现在我得到了解决方案,我已经像那样开发了它

Sub Test()
Dim v As Double, sPath As String, sAPIKey As String, sBase64 As String, cmd As String

sPath = ThisWorkbook.Path & "\Result.txt"
sAPIKey = "APIKEY"
sBase64 = ConvertFileToBase64(ThisWorkbook.Path & "\Logo.png")

cmd = Replace(Replace("curl --location --request POST ""https://api.imgbb.com/1/upload?key=¤"" --form ""image=$"" -o ", "¤", sAPIKey), "$", sBase64) & sPath
v = Shell(cmd)
Debug.Print cmd & " Completed" & vbCr & "Process " & v
End Sub

Public Function ConvertFileToBase64(strFilePath As String) As String
Const UseBinaryStreamType = 1

Dim streamInput: Set streamInput = CreateObject("ADODB.Stream")
Dim xmlDoc: Set xmlDoc = CreateObject("Microsoft.XMLDOM")
Dim xmlElem: Set xmlElem = xmlDoc.CreateElement("tmp")

streamInput.Open
streamInput.Type = UseBinaryStreamType
streamInput.LoadFromFile strFilePath
xmlElem.DataType = "bin.base64"
xmlElem.NodeTypedValue = streamInput.Read
ConvertFileToBase64 = Replace(xmlElem.Text, vbLf, "")

Set streamInput = Nothing
Set xmlDoc = Nothing
Set xmlElem = Nothing
End Function

This works fine for small sizes but not for the large ones ..example I have a Logo.png and the size is 545KB in size and this fails.这适用于小尺寸但不适用于大尺寸..例如我有一个 Logo.png 并且大小为 545KB 并且这失败了。 While other smaller images uploaded well而其他较小的图像上传得很好

I have manually tried it and it failed too.我已经手动尝试过,它也失败了。 it seems cms window has a limit number of characters allowed so I can't get all the base64 string in the command line似乎 cms 窗口允许的字符数有限制,所以我无法在命令行中获取所有 base64 字符串在此处输入图片说明

You can use Shell你可以使用壳牌

Sub mycurl()

  Dim var As Double, cmd As String
  cmd = "curl -I ""https://api.imgbb.com/"" -o imgbb.txt" ' head only
  var = Shell(cmd)
  MsgBox cmd & " completed" & vbCr & "Process " & var

End Sub

Looks like the API accepts binary files so no need to base64 encode.看起来 API 接受二进制文件,所以不需要 base64 编码。 Try尝试

cmd = "curl --location --request POST ""https://api.imgbb.com/1/upload?" & _
      "key=" & sAPIKey & """ " & _
      "--form image=@""/path/Logo.png"" -o " & sPath & "/response.json"



声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM