Guest User

Untitled

a guest
Jun 14th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. function insertFile(fileData, callback) {
  2. const boundary = '-------314159265358979323846';
  3. const delimiter = "rn--" + boundary + "rn";
  4. const close_delim = "rn--" + boundary + "--";
  5.  
  6. var reader = new FileReader();
  7. reader.readAsBinaryString(fileData);
  8. reader.onload = function(e) {
  9. var contentType = fileData.type || 'application/octet-stream';
  10. var metadata = {
  11. 'title': fileData.fileName,
  12. 'mimeType': contentType
  13. };
  14.  
  15. var base64Data = btoa(reader.result);
  16. var multipartRequestBody =
  17. delimiter +
  18. 'Content-Type: application/jsonrnrn' +
  19. JSON.stringify(metadata) +
  20. delimiter +
  21. 'Content-Type: ' + contentType + 'rn' +
  22. 'Content-Transfer-Encoding: base64rn' +
  23. 'rn' +
  24. base64Data +
  25. close_delim;
  26.  
  27. var request = gapi.client.request({
  28. 'path': '/upload/drive/v2/files',
  29. 'method': 'POST',
  30. 'params': {'uploadType': 'multipart'},
  31. 'headers': {
  32. 'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
  33. },
  34. 'body': multipartRequestBody});
  35. if (!callback) {
  36. callback = function(file) {
  37. console.log(file)
  38. };
  39. }
  40. request.execute(callback);
  41. }
  42. }
Add Comment
Please, Sign In to add comment