上传到 Dropbox 但文件很小
Uploading to Dropbox but tiny file size
我在 Excel 中有一个 VBA 函数,它创建一个 document.docx,然后将其上传到 Dropbox。它在 Dropbox 中成功创建了一个文件,但无法打开,因为它只有 2 个字节大小。为什么这个文件里没有内容?我真的不知道文件实际上是如何工作的,它与将内容转换为字节的脚本有关吗?
Public Sub dropboxUpload()
Dim req As MSXML2.ServerXMLHTTP60
Dim strFile As String
Dim Pos1 As Integer
Dim Pos2 As Integer
Set req = New MSXML2.ServerXMLHTTP60
Dim arg As String
Dim content As Byte
content = ReadByteArrFromFile("C:\Users\me\Desktop\Folder\document.docx")
FileName = "document.docx"
arg = "{""path"":""/Work/" & FileName & """,""mode"":{"".tag"":""add""},""autorename"":true,""mute"":false}"
req.Open "POST", "https://content.dropboxapi.com/2/files/upload", False
req.setRequestHeader "Authorization", "Bearer xxxxx"
req.setRequestHeader "Content-Type", "application/octet-stream"
req.setRequestHeader "Content-length", Len(Result)
req.setRequestHeader "Dropbox-API-Arg", arg
req.setRequestHeader "User-Agent", "api-explorer-client"
req.Send content
If req.Status = 200 Then '200 = OK
'MsgBox req.Status & ": " & req.StatusText
Debug.Print req.responseText
Else
MsgBox req.Status & ": " & req.StatusText
Debug.Print req.responseText
End If
End Sub
Function ReadByteArrFromFile(filePath) As Byte()
Dim buff() As Byte
Dim fileNumb As Integer
fileNumb = FreeFile
Open filePath For Binary Access Read As fileNumb
ReDim buff(0 To LOF(fileNumb) - 1)
Get fileNumb, , buff
Close fileNumb
ReadByteArrFromFile = buff
End Function
尝试设置正确的 Content-length
值:
Dim arg As String
Dim content As Byte, fPath as string
fPath = "C:\Users\me\Desktop\Folder\document.docx"
FileName = "document.docx"
arg = "{""path"":""/Work/" & FileName & """,""mode"":{"".tag"":""add""},""autorename"":true,""mute"":false}"
req.Open "POST", "https://content.dropboxapi.com/2/files/upload", False
req.setRequestHeader "Authorization", "Bearer xxxxx"
req.setRequestHeader "Content-Type", "application/octet-stream"
req.setRequestHeader "Content-length", FileLen(fPath) '<<< set the correct size
req.setRequestHeader "Dropbox-API-Arg", arg
req.setRequestHeader "User-Agent", "api-explorer-client"
req.Send ReadByteArrFromFile(fPath)
If req.Status = 200 Then '200 = OK
'MsgBox req.Status & ": " & req.StatusText
Debug.Print req.responseText
Else
MsgBox req.Status & ": " & req.StatusText
Debug.Print req.responseText
End If
End Sub
我在 Excel 中有一个 VBA 函数,它创建一个 document.docx,然后将其上传到 Dropbox。它在 Dropbox 中成功创建了一个文件,但无法打开,因为它只有 2 个字节大小。为什么这个文件里没有内容?我真的不知道文件实际上是如何工作的,它与将内容转换为字节的脚本有关吗?
Public Sub dropboxUpload()
Dim req As MSXML2.ServerXMLHTTP60
Dim strFile As String
Dim Pos1 As Integer
Dim Pos2 As Integer
Set req = New MSXML2.ServerXMLHTTP60
Dim arg As String
Dim content As Byte
content = ReadByteArrFromFile("C:\Users\me\Desktop\Folder\document.docx")
FileName = "document.docx"
arg = "{""path"":""/Work/" & FileName & """,""mode"":{"".tag"":""add""},""autorename"":true,""mute"":false}"
req.Open "POST", "https://content.dropboxapi.com/2/files/upload", False
req.setRequestHeader "Authorization", "Bearer xxxxx"
req.setRequestHeader "Content-Type", "application/octet-stream"
req.setRequestHeader "Content-length", Len(Result)
req.setRequestHeader "Dropbox-API-Arg", arg
req.setRequestHeader "User-Agent", "api-explorer-client"
req.Send content
If req.Status = 200 Then '200 = OK
'MsgBox req.Status & ": " & req.StatusText
Debug.Print req.responseText
Else
MsgBox req.Status & ": " & req.StatusText
Debug.Print req.responseText
End If
End Sub
Function ReadByteArrFromFile(filePath) As Byte()
Dim buff() As Byte
Dim fileNumb As Integer
fileNumb = FreeFile
Open filePath For Binary Access Read As fileNumb
ReDim buff(0 To LOF(fileNumb) - 1)
Get fileNumb, , buff
Close fileNumb
ReadByteArrFromFile = buff
End Function
尝试设置正确的 Content-length
值:
Dim arg As String
Dim content As Byte, fPath as string
fPath = "C:\Users\me\Desktop\Folder\document.docx"
FileName = "document.docx"
arg = "{""path"":""/Work/" & FileName & """,""mode"":{"".tag"":""add""},""autorename"":true,""mute"":false}"
req.Open "POST", "https://content.dropboxapi.com/2/files/upload", False
req.setRequestHeader "Authorization", "Bearer xxxxx"
req.setRequestHeader "Content-Type", "application/octet-stream"
req.setRequestHeader "Content-length", FileLen(fPath) '<<< set the correct size
req.setRequestHeader "Dropbox-API-Arg", arg
req.setRequestHeader "User-Agent", "api-explorer-client"
req.Send ReadByteArrFromFile(fPath)
If req.Status = 200 Then '200 = OK
'MsgBox req.Status & ": " & req.StatusText
Debug.Print req.responseText
Else
MsgBox req.Status & ": " & req.StatusText
Debug.Print req.responseText
End If
End Sub