我一直試圖使用Microsoft Access中的VBA將文件上載到Azure存儲,但迄今爲止沒有成功。使用VBA和MS將文件上傳到Azure Blob存儲XMLHTTP
我有一個很好的搜索,並找到了一些看起來很有希望的代碼,但我無法得到它的工作。看起來像許多其他人一直在尋找類似的解決方案或幫助與VBA一起使用Azure。
這是代碼;
Private Function pvPostFile(sUrl As String, sFileName As String, Optional ByVal bAsync As Boolean) As String
Const STR_BOUNDARY As String = "3fbd04f5-b1ed-4060-99b9-fca7ff59c113"
Dim nFile As Integer
Dim baBuffer() As Byte
Dim sPostData As String
'--- read file
nFile = FreeFile
Open sFileName For Binary Access Read As nFile
If LOF(nFile) > 0 Then
ReDim baBuffer(0 To LOF(nFile) - 1) As Byte
Get nFile, , baBuffer
sPostData = StrConv(baBuffer, vbUnicode)
End If
Close nFile
'--- prepare body
sPostData = "--" & STR_BOUNDARY & vbCrLf & _
"Content-Disposition: form-data; name=""uploadfile""; filename=""" & Mid$(sFileName, InStrRev(sFileName, "\") + 1) & """" & vbCrLf & _
"Content-Type: application/octet-stream" & vbCrLf & vbCrLf & _
sPostData & vbCrLf & _
"--" & STR_BOUNDARY & "--"
'--- post
With CreateObject("Microsoft.XMLHTTP")
.Open "POST", sUrl, bAsync
.SetRequestHeader "Content-Type", "multipart/form-data; boundary=" & STR_BOUNDARY
.Send pvToByteArray(sPostData)
If Not bAsync Then
pvPostFile = .ResponseText
End If
End With
End Sub
Private Function pvToByteArray(sText As String) As Byte()
pvToByteArray = StrConv(sText, vbFromUnicode)
End Function
(感謝 - https://wqweto.wordpress.com/2011/07/12/vb6-using-wininet-to-post-binary-file/)
當我嘗試使用我的Azure存儲URL形式
https://XXXXX.blob.core.windows.net/
和文件名的代碼(C:\ TEMP \ Test.txt的)我得到以下錯誤;
<?xml version="1.0" encoding="utf-8"?><Error><Code>UnsupportedHttpVerb</Code><Message>The resource doesn't support specified Http Verb.
我懷疑標題或發佈數據有問題,而不是VBA,這不是我的區域。
任何幫助非常感謝。
我認爲存在一些問題:1)URL應該是'https:// XXXXX.blob.core.windows.net/container-name/file-name'。2)所有對Azure存儲的請求都應進行身份驗證。我沒有看到你在代碼中的任何地方設置了'Authorization'頭文件。 –
感謝您的評論 - 我會看看這些。 – jhTuppeny