5
votes

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

2
Please edit your question and include the code you are using to upload the file. Ideally include a stackoverflow.com/help/mcveDaImTo
It's a basic upload from Google Drive documentation.Bogu
Try and set MimeType = "application/vnd.google-apps.document"DaImTo
With MimeType = "application/vnd.google-apps.document" I got error 400 bad request. With MimeType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" works, but I can only view a content of file online not edit.Bogu
In V2 there was a command that would tell Google drive to convert the file to a google drive file type upon upload then you could edit it. I have been digging in the documentation i cant seem to find that. Search for something called convert.DaImTo

2 Answers

6
votes

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',
    ]);
1
votes

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