0

The Google drive api seems confusing when it comes to downloading files. I've been able to list all of the files and even downloaded few files but got stuck while downloading Google docs.

Why don't really they have common method for downloading a file but rather two different methods: Get and Export. When I don't need any conversion for the file, Why do I need to specify the Mime type for Export? Also, while listing files, the Mime type for all the files is always null. e.g

@foreach ($files->getFiles() as $file)
    {{  $file->getMimeType() }} // empty string / null
@endforeach

Reference to getMimeType

Here's what I've done so far

public function listFiles(Request $request)
  client = $this->getClient();
  $service = new Google_Service_Drive($client);
  $optParams = array(
    'q' => 'fullText contains \'"abc"\'',
    'pageSize' => 10,
    'fields' => 'nextPageToken, files(id, name)'
  );
  $files = $service->files->listFiles($optParams);
}
public function downloadFile($file_id)
{
    $client = $this->getClient();
    $service = new Google_Service_Drive($client);
    $file = null;
    $file = $service->files->export($file_id, 'application/pdf', array(
            'alt' => 'media'
    )); // I really need to get rid of Mimetype(application/pdf), It should download the exact same file
}

Update

I have managed to solve the mime type problem by providing the field in the list.

$optParams = array(
        'q' => 'fullText contains \'"basheer"\'',
        'pageSize' => 10,
        'fields' => 'nextPageToken, files(id, name, mimeType)' //<--
    );

But now when I try to download load the file, it throws the following error

The requested conversion is not supported. "domain": "global", "reason": "badRequest",

// request('mimeType') = application/vnd.google-apps.spreadsheet
$file = $service->files->export($file_id, request('mimeType'), array(
            'alt' => 'media'
        ));

Your answers and comments will be appreciated.

Thanks

Basheer Kharoti
  • 4,202
  • 5
  • 24
  • 50

0 Answers0