Skip to content

Instantly share code, notes, and snippets.

@hayk94
Created February 23, 2017 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hayk94/2a11702a3476f49bd65e36e7fa715d90 to your computer and use it in GitHub Desktop.
Save hayk94/2a11702a3476f49bd65e36e7fa715d90 to your computer and use it in GitHub Desktop.
Upload file to dropbox with Meteor 1.4 FEB 2017 Simple way
// Put this file in the server, not in the client, not in the imports
import { Meteor } from 'meteor/meteor'
import Dropbox from 'dropbox'
import FileAPI from 'file-api'
const { File } = FileAPI
console.log('dropbox settings', Meteor.settings.dropbox)
const dbx = new Dropbox({accessToken: Meteor.settings.dropbox.accessToken})
Meteor.methods({
'uploadToDropbox': function (base64File) {
console.log('base64File', base64File.slice(5))
const i = base64File.indexOf('base64,')
const buffer = Buffer.from(base64File.slice(i + 7), 'base64')
const name = `${Math.random().toString(36).slice(-5)}.png`
const file = new File({buffer, name, type: 'image/png'})
console.log('file', file)
dbx.filesUpload({path: '/' + file.name, contents: file.buffer})
.then(function (response) {
console.log(response)
})
.catch(function (error) {
console.error('dropbox error', error)
})
return false
}
})
// Will make this file simpler in the future, however for now...
// Get the file with HTML 5 or React Dropzone
uploadCallback (file) {
console.log('file', file)
this.getBase64(file, this)
}
getBase64 (file, self) {
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = function () {
console.log('reader.result', reader.result)
self.sendFileToServer(reader.result)
}
reader.onerror = function (error) {
console.log('Error: ', error)
}
}
sendFileToServer (base64File) {
return new Promise(
(resolve, reject) => {
console.log('uploadCallback promise')
Meteor.call('uploadToDropbox', base64File, function (error, result) {
console.log('uploadToDropbox callback')
if (error) {
console.log('error', error)
}
if (result) {
console.log('result', result)
}
})
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment