I am using the Google Drive SDK to upload files from javascript (https://developers.google.com/drive/v2/reference/files/insert, using uploadType multipart to specify both content and metadata).
I can upload images (mimeType image/jpeg) fine. However, uploading plain text (mimeType text/plain) or JSON (mimeType application/json) files would result in an empty file being created.
My work around was to specify the file content mimeType as 'application/octet-stream' and everything works fine.
Anyone have any idea why specifying a mimeType of application/json for json content, or text/plain for text content would result in an empty file while changing to application/octet-stream works fine?
I should note that because my code is running as a part of a Chrome App, I have had to make a modified gapi client (since the standard one violates CORS). As an aside, is the google javascript api (gapi) client code available in a code repository somewhere to check out?
An excerpt of my upload code is:
var metadata = {
'title': ufilename,
'mimeType': contentType,
'modifiedDate' : modifiedDate,
'parents': [{'id':parentGuid}]
};
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: ' + contentType + '\r\n' +
'\r\n' +
JSON.stringify(jsonObj) +
close_delim;
var reqObj = {
'path': '/upload/drive/v2/files',
'method': 'POST',
'params': {'uploadType': 'multipart'},
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
'body': multipartRequestBody
};
var request = gapi.client.request(reqObj);
As requested, a sample request is:
headers: Content-Type: "multipart/mixed; boundary="-------314159265358979323846"
method: "POST"
params: uploadType: "multipart"
path: "https://www.googleapis.com/upload/drive/v2/files"
body:
---------314159265358979323846
Content-Type: application/json
{"title":"survey.json","mimeType":"application/octet-stream","modifiedDate":"2014-10-24T08:17:45.099-06:00","parents":[{"id":"0B4ho7vdmkgJOczc4NENqd0d0TDg"}]}
---------314159265358979323846
Content-Type: application/octet-stream
{"model":{"DateTime":{"type":"datetime","required":true},"Bird":{"type":"string"},"Photo": {"type":"image"},"Notes":{"type":"string"},"Location":{"type":"geopoint"}},"settings":[{"setting":"form_id","value":"birdwatching"},{"setting":"form_version","value":1},{"setting":"form_category","value":"Tracking"},{"setting":"form_title","value":"Bird Watching"}],"survey":[{"name":"DateTime","type":"datetime","label":"Date and Time"},{"name":"Location","type":"geopoint","label":"Location"},{"name":"Bird","type":"string-auto","label":"Bird"},{"type":"text","name":"Notes","label":"Notes"},{"name":"Photo","type":"image","label":"Photo"}],"choices":{}}
---------314159265358979323846--
In all cases (regardless of mime-type) I get a return object with readyState 4/status 200. The only difference is the fileSize is 0 if Content-Type is I specify mimetype of application/json.