我正在尝试使用 VBA for MS Access 中的 Microsoft.XMLHTTP 通过进度跟踪将文件上传到存储。没有进度跟踪的上传工作正常,但我需要进度跟踪,特别是对于较大的文件。
我对这条线有疑问:
xmlHttp.send fileData
err: 参数不正确... 我尝试将文件分成 5 部分并上传,但我做错了。 这是片段:
Public Sub UploadToAzureBlob(filePath As String, fileName As String)
Dim adoStream As Object
Dim xmlHttp As Object
Dim responseStatus As Long
Dim sUrl As String
Dim fileSize As Long
Dim bytesSent As Long
Dim chunkSize As Long
Dim fileData() As Byte
Dim progressForm As Form
Dim numParts As Long
'On Error GoTo ErrHandler
filePath = filePath & fileName
fileName = "/" & URLEncodeJScript(fileName) ' For Azure in this format
sUrl = blobUrl & fileName & sasToken
Set adoStream = CreateObject("ADODB.Stream")
adoStream.Mode = 3
adoStream.Type = 1
adoStream.Open
adoStream.LoadFromFile filePath
fileSize = adoStream.Size
numParts = 5
chunkSize = fileSize \ numParts
If chunkSize = 0 Then chunkSize = fileSize
DoCmd.OpenForm "dlgPRGBAR"
Set progressForm = Forms!dlgPRGBAR
Set xmlHttp = CreateObject("Microsoft.XMLHTTP")
bytesSent = 0
Do While bytesSent < fileSize
If bytesSent + chunkSize > fileSize Then
chunkSize = fileSize - bytesSent
End If
adoStream.Position = bytesSent
ReDim fileData(0 To chunkSize - 1)
fileData = adoStream.Read(chunkSize)
Debug.Print "Chunk size: " & chunkSize & " Bytes sent: " & bytesSent
xmlHttp.Open "PUT", sUrl, False
xmlHttp.setRequestHeader "x-ms-blob-type", "BlockBlob"
xmlHttp.setRequestHeader "Content-Length", CStr(chunkSize)
Debug.Print "URL: " & sUrl
Debug.Print "Content-Length: " & CStr(chunkSize)
xmlHttp.send fileData
If xmlHttp.status <> 201 And xmlHttp.status <> 202 Then
Debug.Print "Error: " & xmlHttp.status & " - " & xmlHttp.StatusText
MsgBox "Error: " & xmlHttp.status & " - " & xmlHttp.StatusText, vbCritical
GoTo CleanUp
End If
bytesSent = bytesSent + chunkSize
Dim IntValue As Long
IntValue = (bytesSent \ chunkSize)
If IntValue >= 5 Then
IntValue = 5
End If
Set prg = Forms!dlgPRGBAR!CtlProgress.Object
Set Complete = Forms!dlgPRGBAR!lblComplete
prg.Max = numParts
prg.Value = IntValue
strComplete = Format((prg.Value / prg.Max) * 100, "##") & " % Complete"
Complete.Caption = strComplete
DoCmd.RepaintObject
Loop
adoStream.Close
DoCmd.Close acForm, "dlgPRGBAR"
responseStatus = xmlHttp.status
If responseStatus = 201 Then
MsgBox "File uploaded successfully!", vbInformation
Else
MsgBox "Error: " & responseStatus & " - " & xmlHttp.StatusText, vbCritical
End If
CleanUp:
On Error Resume Next
If Not adoStream Is Nothing Then adoStream.Close
Set adoStream = Nothing
Set xmlHttp = Nothing
Exit Sub
ErrHandler:
MsgBox "An error occurred: " & err.Description, vbCritical
Resume CleanUp
End Sub
根据 Azure 文档:
“Put Blob 不支持部分更新。要对块 blob 的内容执行部分更新,请使用 Put Block List 操作。”
不幸的是,我用这个方法没有成功。
与此同时,我探索了如何使用Append Blob 操作并成功地部分上传了文件。虽然这种方法有效,但我不确定它是否是正确的方法,特别是在文件上传期间合并进度条。
因此,我想澄清执行部分上传到Azure的适当方法。我应该使用 Put Block List 操作,还是可以使用 Append Blob 操作?
下面是演示 Append Blob 操作用法的片段: 注意:新的附加 blob 必须创建为容器中的新条目; 它无法覆盖任何现有的 blob。