In my MS Access database i want to upload my customer profile photo to my google drive using HTTPS Post request with google drive API in VBA. i successfully uploaded photo in my google drive but the format is not supported. For uploading photo using https post request i need to convert the image into Base64 string. After uploading i can't see or open the photo until i download the photo and rename the file extension from "jpg" to "txt". After renaming the file i can open the file in notepad and see the Base64 string. if i convert the Base64 string then i can see the photo. How can i convert Base64 image? i use HTML img tag like that
<!DOCTYPE html><head></head><body><img src="data:image/jpg;base64,/9j/4QEcRXhpZgAATU0AKgA(This is the Base64 text................" alt=""> </body></html>
this way i can see the photo. My VBA code for uploading image to google drive is
Option Compare DatabaseSub UploadFileToGoogleDrive71() Dim imageFile As String Dim imageBytes() As Byte Dim base64String As String Dim boundary As String Dim request As Object Dim accessToken As String' Your access token accessToken = "ya29.a0Ad52N3_EtFDYr_3lTO-i1P0sNbqgUXzvp..........."' Path to your image file imageFile = Forms!PatientFormF2!PatientPhotoPath.Value' Read the image file into a byte array Open imageFile For Binary As #1 ReDim imageBytes(LOF(1) - 1) Get #1, , imageBytes Close #1' Encode the byte array as a Base64 string base64String = EncodeBase64(imageBytes)' Construct the boundary for the multipart/form-data request boundary = "---------------------------" & Format(Now, "hhmmss") & "abcd"' Create the HTTP request object Set request = CreateObject("MSXML2.XMLHTTP") Dim folderId As String' Set the folder ID where you want to upload the photos folderId = "1EptP5DEg_m2DE1N67sQ........"' Set up the request request.Open "POST", "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart", False request.setRequestHeader "Content-Type", "multipart/form-data; boundary=" & boundary request.setRequestHeader "Content-Length", Len(postData) ' Set the Content-Length header request.setRequestHeader "Authorization", "Bearer " & accessToken ' Set the Authorization header' Construct the request payload Dim requestData As String requestData = "--" & boundary & vbCrLf requestData = requestData & "Content-Type: application/json; charset=UTF-8" & vbCrLf & vbCrLf requestData = requestData & "{""name"": ""uploaded_image.jpg"", ""parents"": [""" & folderId & """]}" & vbCrLf & vbCrLf requestData = requestData & "--" & boundary & vbCrLf requestData = requestData & "Content-Type: image/jpeg" & vbCrLf & vbCrLf requestData = requestData & base64String & vbCrLf requestData = requestData & "--" & boundary & "--"' Send the request request.Send requestData' Check the response If request.status = 200 Then MsgBox "File uploaded successfully!" Else MsgBox "Error uploading file: " & request.StatusText End IfEnd SubFunction EncodeBase64(data() As Byte) As String Dim objXML As Object Set objXML = CreateObject("MSXML2.DOMDocument") Dim objNode As Object' Convert byte array to base64 string Set objNode = objXML.createElement("b64") objNode.DataType = "bin.base64" objNode.nodeTypedValue = data EncodeBase64 = objNode.Text Set objNode = Nothing Set objXML = NothingEnd Function
i want to upload image from my MS Access database using google drive API and able to see the photo directly in the google drive and also able to see the photo by downloading them. Thanks in Advance for any help.