Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload/convert *.docx to google documents? Google Drive Api v3

I have problem with upload a microsoft/libre office document for edit via google apps. For spreadsheet it's working properly but for docs isn't.

When I upload a file with extension like *.odt, *.docx I can see the content by google viewer on Google Drive but when I click edit using button on top of document the Google Drive creates a new file with the same content and name but I don't have information about original file ID in description or any capitability. Any ideas how to upload a document ready for edit online?

  private async Task<string> UploadFileToGoogleDrive(string filePath, GoogleDriveFile file)
    {
        var fileData = new Google.Apis.Drive.v3.Data.File()
        {
            Name = file.Name,
            MimeType = file.MimeType

        };
        if (!String.IsNullOrEmpty(file.ParentId))
            fileData.Parents = new List<string>() { file.ParentId };

        FilesResource.CreateMediaUpload request;

        using (var stream = new FileStream(filePath, FileMode.Open))
        {
             request = _driveService.Files.Create(fileData, stream, StringEnum.GetStringValue(file.FileContent));
             request.Fields = "id";

             var uploadProgress = await request.UploadAsync();
             if (uploadProgress.Exception != null)
                 throw uploadProgress.Exception; 
        }
        return request.ResponseBody.Id;
    }

File mime types https://developers.google.com/drive/v3/web/manage-downloads

Or with "importFormats" parameter. https://developers.google.com/drive/v3/reference/about/get

like image 370
Bogu Avatar asked Jan 17 '18 11:01

Bogu


People also ask

Can you upload docx to Google Drive?

Go to File > Download and select a file format from the menu that appears. Choose from formats such as DOCX, ODT, RTF, PDF, EPUB, and others. Choose a folder where the document should be saved. It may also directly download to your computer if you've defined a download folder for your browser.

How do I convert multiple docx files to Google Docs?

In the Settings window, enable the “Convert uploaded files to Google Docs editor format” option. Then, at the top-right of the window, click “Done.” Next, in the Google Drive sidebar, click “New.” Then, select “File Upload,” and upload all your Word (or other Office) files that you want to convert.


2 Answers

I also suffered for a long time. In order for Google to convert to the format we need, we specify it in the MimeType in meta-data. You also need to specify the current file format in MimeType in the content object.

There is an example only on PHP

$fileMetadata = new \Google_Service_Drive_DriveFile([
        'name' => $this->_fileName,
        'parents' => array($directoryId),
        'mimeType' => 'application/vnd.google-apps.document'
    ]);

    return $this->_service()->files->create($fileMetadata, [
        'data' => $document,
        'mimeType' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        'uploadType' => 'multipart',
        'fields' => 'id',
    ]);
like image 143
Dangetsu Kami-sama Avatar answered Oct 19 '22 22:10

Dangetsu Kami-sama


Here it is in C# (I followed this example from https://developers.google.com/drive/api/v3/manage-uploads#import_to_google_docs_types_

You should be able to put the code within the using statement for the file stream

    using File = Google.Apis.Drive.v3.Data.File; // This obviously goes at the top of the file, alternatively you can fully qualify the File constructor, although I had ambiguity issued due to also `using System.IO` and the compiler not knowing which `File` I meant specifically. 

    ...

    var fileMetadata = new File() // please note this is Google.Apis.Drive.v3.Data.File;
    {
        Name = "New File Name I Want"
        MimeType = "application/vnd.google-apps.document"
    };

    var request = service.Files.Create(
        fileMetadata, 
        stream, 
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document" // please note this is the mimeType of the source file not the tartget
    ); 
    request.Fields = "id";
    var result = await request.UploadAsync();

According to the documentation, it is possible to do this with an Update action rather than an Upload, but this will convert the original file and the original will be lost. Unfortunately there is no example given about how to do this and the API seems to be very fussy over what's passed in. Will update this answer to include if I ever figure it out

like image 31
Max Carroll Avatar answered Oct 20 '22 00:10

Max Carroll