【发布时间】:2015-10-22 16:13:44
【问题描述】:
我一直在尝试使用 Microsoft Access 中的 VBA 将文件上传到 Azure 存储,但到目前为止没有成功。
我进行了很好的搜索,发现了一些看起来很有希望的代码,但我无法让它工作。似乎许多其他人一直在寻找类似的解决方案或帮助从 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标头。 -
感谢您的评论 - 我会看看这些。
标签: vba ms-access azure http-headers blob