I’m working with MS Access and trying to upload customer images to Google Drive through their API using VBA. The upload process works but there’s a formatting issue that’s driving me crazy.
When I upload the image file, it appears in my Google Drive but can’t be opened as a normal image. To actually see the photo, I have to download it first and change the file extension from jpg to txt. Then I can open it in Notepad and see it’s just a Base64 string. The only way I can view the actual image is by putting this Base64 string into an HTML file like this:
<!DOCTYPE html>
<html>
<head></head>
<body>
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAABAAEDASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAv/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/8QAFQEBAQAAAAAAAAAAAAAAAAAAAAX/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwA/AD/2Q==" alt="Photo">
</body>
</html>
Here’s my VBA code that handles the upload:
Option Compare Database
Sub TransferImageToGoogleDrive()
Dim photoPath As String
Dim fileData() As Byte
Dim encodedImage As String
Dim requestBoundary As String
Dim httpRequest As Object
Dim authToken As String
authToken = "ya29.a0Ad52N3_EtFDYr_3lTO-i1P0sNbqgUXzvp..........."
photoPath = Forms!CustomerForm!ImagePath.Value
Open photoPath For Binary As #1
ReDim fileData(LOF(1) - 1)
Get #1, , fileData
Close #1
encodedImage = ConvertToBase64(fileData)
requestBoundary = "---------------------------" & Format(Now, "hhmmss") & "xyz"
Set httpRequest = CreateObject("MSXML2.XMLHTTP")
Dim targetFolder As String
targetFolder = "1EptP5DEg_m2DE1N67sQ........"
httpRequest.Open "POST", "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart", False
httpRequest.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & requestBoundary
httpRequest.setRequestHeader "Content-Length", Len(payloadData)
httpRequest.setRequestHeader "Authorization", "Bearer " & authToken
Dim payloadData As String
payloadData = "--" & requestBoundary & vbCrLf
payloadData = payloadData & "Content-Type: application/json; charset=UTF-8" & vbCrLf & vbCrLf
payloadData = payloadData & "{""name"": ""customer_photo.jpg"", ""parents"": [""" & targetFolder & """]}" & vbCrLf & vbCrLf
payloadData = payloadData & "--" & requestBoundary & vbCrLf
payloadData = payloadData & "Content-Type: image/jpeg" & vbCrLf & vbCrLf
payloadData = payloadData & encodedImage & vbCrLf
payloadData = payloadData & "--" & requestBoundary & "--"
httpRequest.Send payloadData
If httpRequest.status = 200 Then
MsgBox "Upload completed successfully!"
Else
MsgBox "Upload failed: " & httpRequest.StatusText
End If
End Sub
Function ConvertToBase64(byteArray() As Byte) As String
Dim xmlDoc As Object
Set xmlDoc = CreateObject("MSXML2.DOMDocument")
Dim xmlElement As Object
Set xmlElement = xmlDoc.createElement("encode")
xmlElement.DataType = "bin.base64"
xmlElement.nodeTypedValue = byteArray
ConvertToBase64 = xmlElement.Text
Set xmlElement = Nothing
Set xmlDoc = Nothing
End Function
What am I doing wrong here? I want the images to upload properly so they can be viewed directly in Google Drive without all this extra work.