Skip to main content
Raffus kn ha preguntado en #Developer Forums

This is my code for uploading a file in Google Drive folder-

I have a folder Id. 

Currently, the folder and file is created but the file is being created outside of the folder. The file should be created in the folder.

Also file name is created 'Untitled' despite setting the FileName.

 

ContentDocument document = [SELECT Title FROM ContentDocument WHERE Id IN (SELECT ContentDocumentId FROM ContentVersion WHERE Id = :cvId)];

String fileName = document.Title;

System.debug('file Name '+ fileName);

// Prepare request to Google Drive to upload file to folder

String url = 'https://www.googleapis.com/upload/drive/v2/files?uploadType=media&parents=' + folderId;

String authorizationHeader = 'Bearer ' + accessToken;

HttpRequest req1 = new HttpRequest();

req1.setHeader('Authorization', authorizationHeader);

req1.setHeader('Content-Type', 'application/pdf'); // Adjust content type as needed

req1.setheader('Content-Type','image/'+'PNG');

req1.setMethod('POST');

req1.setEndpoint(url);

req1.setHeader('Content-Disposition', 'attachment; filename="' + fileName + '"');

req1.setBodyAsBlob(cv.VersionData);

req1.setHeader('Content-Length', String.valueOf(cv.VersionData.size()));

// Send request to Google Drive

Http h1 = new Http();

HttpResponse resp1 = h1.send(req1);

 

#Developer Forums  #Apex  #Integration

3 respuestas
  1. 28 feb 2024, 11:13

    @Raffus kn

    • To upload a file with specific metadata (like name and parents), you should use a multipart upload, which combines file metadata and the file data in a single request. This approach allows you to specify the file name and parent folder ID in the metadata part of the request.

    ContentDocument document = [SELECT Title FROM ContentDocument WHERE Id IN (SELECT ContentDocumentId FROM ContentVersion WHERE Id = :cvId)];

    String fileName = document.Title;

    System.debug('file Name '+ fileName);

    // Prepare the metadata for the file, including the name and parent folder ID

    String metadata = JSON.serialize(new Map<String, Object>{

    'name' => fileName,

    'parents' => new List<String>{folderId}

    });

    String boundary = '-------314159265358979323846';

    String delimiter = '\r\n--' + boundary + '\r\n';

    String close_delim = '\r\n--' + boundary + '--';

    String contentType = 'application/pdf'; // Adjust this based on your file type

    // Combine the metadata and file content into a single request body

    String requestBody = delimiter +

    'Content-Type: application/json\r\n\r\n' +

    metadata + '\r\n' +

    delimiter +

    'Content-Type: ' + contentType + '\r\n' +

    'Content-Transfer-Encoding: base64\r\n\r\n' +

    EncodingUtil.base64Encode(cv.VersionData) +

    close_delim;

    // Prepare the HttpRequest

    HttpRequest req = new HttpRequest();

    req.setHeader('Authorization', 'Bearer ' + accessToken);

    req.setHeader('Content-Type', 'multipart/related; boundary="' + boundary + '"');

    req.setMethod('POST');

    req.setEndpoint('https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart');

    req.setHeader('Content-Length', String.valueOf(requestBody.length()));

    req.setBody(requestBody);

    // Send the request

    Http http = new Http();

    HttpResponse response = http.send(req);

    System.debug(response.getBody());

    replace application/pdf with the correct MIME type of your file and adjust the metadata as necessary.

0/9000