nycynik nycynik - 1 year ago 160
Javascript Question

Google Drive API - JavaScript Insert File - Create native google doc

I have been working with the JavaScript Google Drive API in order to create (insert) a new file. It works great, except that the document it creates is not a google doc, but some kind of document that can be converted in to a google doc.

I would like an actual google doc. I tried "convert:true", but it does not seem to convert it to a doc.

insertFile: function (filename, content, callback) {
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";

var contentType = 'application/';
var metadata = {
'title': filename,
'mimeType': 'text/html',
'description': 'Created'

// TODO: replace this with a library -
var base64Data = window.btoa(unescape(encodeURIComponent(content)));
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: ' + contentType + '\r\n' +
'Content-Transfer-Encoding: base64\r\n' +
'\r\n' +
base64Data +

var request = gapi.client.request({
'path': '/upload/drive/v2/files',
'method': 'POST',
'params': {'uploadType': 'multipart', 'convert': true },
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
'body': multipartRequestBody
if (!callback) {
callback = function (file) {

After I insert the file, when I go to the link I get:

Image of google drive screen

Answer Source

I think I found your error. Try migrating to v3, according to v3 migration:

Other changes

Imports to Google Docs formats are now requested by setting the appropriate target mimeType in the resource body, rather than specifying ?convert=true.

Import operations will return a 400 error if the format is not supported.

And here is the link for the list of supported mimeType.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download