Is there any way to get the content of native files (Google Docs) using Google Drive API v3? I know API v2 supports this with the exportLinks property, but it doesn't work anymore or has been removed.
You can also download files with binary content (non google drive file) in Drive using the webContentLink
attribute of a file. From https://developers.google.com/drive/v3/reference/files:
A link for downloading the content of the file in a browser. This is only available for files with binary content in Drive.
An example (I use method get()
to retrieve the webContentLink
from my file):
gapi.client.drive.files.get({
fileId: id,
fields: 'webContentLink'
}).then(function(success){
var webContentLink = success.result.webContentLink; //the link is in the success.result object
//success.result
}, function(fail){
console.log(fail);
console.log('Error '+ fail.result.error.message);
})
With google drive files, the export method can be used to get those files: https://developers.google.com/drive/v3/reference/files/export
This method needs an object with 2 mandatory attributes (fileId
, and mimeType
) as parameters.
A list of available mimeType
s can be seen here or here (thanks to @ravioli)
Example:
gapi.client.drive.files.export({
'fileId' : id,
'mimeType' : 'text/plain'
}).then(function(success){
console.log(success);
//success.result
}, function(fail){
console.log(fail);
console.log('Error '+ fail.result.error.message);
})
You can read non google doc file content (for example a text file) with gapi.client.drive.files.get
with alt:"media"
. Official example. My example:
function readFile(fileId, callback) {
var request = gapi.client.drive.files.get({
fileId: fileId,
alt: 'media'
})
request.then(function(response) {
console.log(response); //response.body contains the string value of the file
if (typeof callback === "function") callback(response.body);
}, function(error) {
console.error(error)
})
return request;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With