javascript


How to upload blob pdf file on google drive using javascript


I am trying to upload application/pdf blob file converted to base64 on google drive. The file gets uploaded to drive but with empty content. Check result snap for details on how it looks after uploading to drive.
function postImageToDrive(blob, metadata) {
const reader = new FileReader();
reader.readAsArrayBuffer(blob);
return new Promise(resolve => {
reader.onload = function() {
const contentType = blob.type || 'application/octet-stream';
const base64Data = btoa(arrayBufferToString(reader.result));
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const closeDelim = "\r\n--" + boundary + "--";
const 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 +
closeDelim;
return googleDrive.init().then(gapi => gapi.client.request({
'path': '/upload/drive/v2/files',
'method': 'POST',
'params': {
'uploadType': 'multipart',
},
'headers': {
'Content-Type': `multipart/mixed; boundary=${boundary}`,
},
'body': multipartRequestBody,
})).then(resolve);
};
});
}
Result:

Related Links

Use of singlequotes when writing HTML using javascript
How to Override views,controllers,stylesheets, and javascripts for whole application through rails plugin
Ordering duplicates in an array
Dynamic absolute position divs won't resize section
Is there a way to render a view only once in ExpressJS?
xcode 5, I would like to remove a class in uiWebView
jquery selector type text or password [duplicate]
Navigate to page that is inside div tag in JQuery Mobile
knockout.js table filter doesn't update
How do I listen to touches/swipes inside an iFrame?
insertRule and addRule
Transparent texture not fully opaque
Div to fade in at certain point on scroll
iOS 7 - Automatically close select dropdown menu onchange with jQuery
Display a DIV content centered in a page with a scroll
Use jQuery to sort a list into groups with headings

Categories

HOME
python-3.x
ubuntu
web-scraping
hybris
version-control
odoo-9
dotnetbrowser
ads
datagridview
dspace
drm
alias
tagging
frames
google-custom-search
android-tablayout
patch
passport.js
blur
canjs
smartadmin
grav
pg-promise
visual-c++-2015
nswindow
vaadin8
gnu-parallel
google-admob
ncrunch
pusher
lte
openjpa
wrapper
silktest
screeps
cocos2d-x-win32
perfect
iglistkit
libtorrent
file-transfer
shiny-server
tfs2012
flash-cs6
scntext
data-files
stemming
analog-digital-converter
alteryx
page-break
peewee
url-rewrite-module
feedparser
wincvs
switchcompat
google-app-engine-python
sitecore-pxm
lv2
django-cache
bootstrap-timepicker
array-algorithms
classnotfound
scrollreveal.js
dev-to-production
mongodump
app-router
touch-id
reactjs-flux
appimage
public-key
application-settings
xcode6
scala-java-interop
mod-mono
sunone
ashx
foreign-key-relationship
couchbase-java-api
actionmailer
payflowpro
avrdude
phase
cabasicanimation
unresolved-external
cocoaasyncsocket
resharper-8.0
windows-live
user-feedback
weblogic8.x
klout
user-extensions.js
jcolorchooser
elevated-privileges
autoloader
webformsmvp
messageformat
fcntl

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App