Last active
August 11, 2016 09:17
-
-
Save bachvtuan/7959960bff1eb5cbe1f089d444ecebfa to your computer and use it in GitHub Desktop.
[ Nodejs ]Upload unicode file to Dropbox V2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request'); | |
var fs = require('fs'); | |
//Reference https://www.dropboxforum.com/hc/en-us/community/posts/207458626-HTTP-header-Dropbox-API-Arg-could-not-decode-input-as-JSON | |
var charsToEncode = /[\u007f-\uffff]/g; | |
function http_header_safe_json(v) { | |
return JSON.stringify(v).replace(charsToEncode, | |
function(c) { | |
return '\\u'+('000'+c.charCodeAt(0).toString(16)).slice(-4); | |
} | |
); | |
} | |
var full_path = "/path/with/unicode/nội dung unicode"; | |
var access_token = "xxxxxxxxxxxxxxxxxxx"; | |
fs.readFile("/path/your/file", function read(err, data) { | |
if (err) { | |
throw err; | |
} | |
//common.log("Data ", data); | |
//var content = data; | |
var params = { | |
path:full_path, | |
"mode": "add", | |
"autorename": true, | |
"mute": false | |
} | |
var post_params = { | |
url: 'https://content.dropboxapi.com/2/files/upload', | |
method: "POST", | |
//json: true, | |
headers: { | |
'Authorization': 'Bearer ' + access_token, | |
'Dropbox-API-Arg': http_header_safe_json(params), | |
'Content-Type': 'text/plain; charset=dropbox-cors-hack' | |
}, | |
body:data | |
} | |
// | |
request( post_params , function optionalCallback(error, response, body) { | |
console.log("body",body); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment