Hi
Can someone tell me how to upload a JPEG picture from nodejs? I know that with the Javascript SDK the functionality is not supported yet, I am using the REST API and sending an array of byte in this way:
var options = {
host: ‘api.backendless.com’,
path: ‘/’ + backend.version + ‘/files/’ + user.login + ‘/’ + file,
method: ‘PUT’,
headers: {
‘application-id’: backend.applicationId,
‘secret-key’: backend.secretRestKey,
‘Content-Type’: req.body.fileMimeType,
‘Content-Length’ : req.body.fileSize,
‘application-type’: ‘REST’,
‘user-token’ : req.session.user[‘user-token’]
}
};
var request = http.request(options, function(response) {
response.setEncoding('utf8');
response.on('data', function (data) {
if (response.statusCode == '200') {
user.avatar = JSON.parse(data).fileURL;
console.log(user.avatar);
backend.backendless.UserService.register(user).then(function(){
// success
res.redirect('/register?scode=200');
}).catch(function(error){
// error
res.redirect('/register?scode=' + error.code + '&detail=' + error.message);
-
console.log('User creation failed: ' + JSON.stringify(error)); }); } else { console.log(data); res.render('login', {"error" : JSON.parse(data)}); } });
}, function(error){
console.log(‘Error is’ + error);
res.redirect(’/register?scode=’ + error.code + ‘&detail=’ + error.message);
});request.write(inputFile);
request.end();
This is what I see in the console for the options array:
{ host: ‘api.backendless.com’,
path: ‘/v1/files/kaba@gmail.com/1463792576285-img050.jpg’,
method: ‘PUT’,
headers:
{ ‘application-id’: ‘67B8DFF8-281D-7293-FF34-E2B84A032F00’,
‘secret-key’: ‘DCA77BB2-BEC5-DEE5-FFC0-DEC02DE0E300’,
‘Content-Type’: ‘image/jpeg’,
‘Content-Length’: ‘78116’,
‘application-type’: ‘REST’,
‘user-token’: ‘4DF601DE-2CEE-74E1-FFBA-BF8E0A174300’ } }
The server never responds back
Thanks.