I'm trying to upload a file from a node application to my local Alfresco. I managed to login, create and delete folders but not files.
let AlfrescoApi = require('alfresco-js-api');
let alfrescoJsApi = new AlfrescoApi();
let fs = require('fs');
alfrescoJsApi.login('admin', 'admin').then(function (data) {
console.log('API called successfully login ticket:' + data);
var fileToUpload = fs.createReadStream('./testFile.txt');
fileToUpload.name= "testFile.txt"
alfrescoJsApi.upload.uploadFile(fileToUpload, 'Sites/test-site/documentLibrary')
.then(function () {
console.log('File Uploaded in the root');
}, function (error) {
console.log('Error during the upload' + error);
});
}, function (error) {
console.log("Error, cannot connect to Alfresco");
});
The previous code return the error :
Error during the uploadError: {"error":{"errorKey":"Required parameters are missing","statusCode":400,"briefSummary":"05010132 Required parameters are missing","stackTrace":"Pour des raisons de sécurité, le traçage de la pile n'est plus affiché, mais la propriété est conservée dans les versions précédente.","descriptionURL":"https://api-explorer.alfresco.com"}}
And I don't know what I am doing wrong, I tried every methods with differents parameters listed here : https://www.npmjs.com/package/alfresco-js-api#upload-file but always got the same error... If anyone could help me it would be great, thanks =)
EDIT : So I gave up this method, and start trying directly with rest requests, I managed to write this piece of code :
var http = require("http");
var options = {
'host': 'localhost',
'port': '8080',
'path': '/alfresco/service/api/upload?alf_ticket='+ticket,
'method': 'POST',
'Content-Type': 'application/json'
};
var fs = require('fs')
var fileToUpload = fs.createReadStream('./testFile.txt');
var body = {
"filedata": fileToUpload,
"filename": "testFile.txt",
"description": "none",
"siteid": "test-site",
"containerid": "documentLibrary"
}
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.write(JSON.stringify(body));
req.end();
But, now, I have an error 500 ...
STATUS: 500
HEADERS: {"server":"Apache-Coyote/1.1","cache-control":"no-cache","expires":"Thu, 01 Jan 1970 00:00:00 GMT","pragma":"no-cache","content-type":"application/json;charset=UTF-8","transfer-encoding":"chunked","date":"Thu, 01 Jun 2017 14:20:43 GMT","connection":"close"}
BODY: {
"status" :
{
"code" : 500,
"name" : "Internal Error",
"description" : "An error inside the HTTP server which prevented it from fulfilling the request."
},
"message" : "05010287 Unexpected error occurred during upload of new content.",
"exception" : "",
"callstack" :
[
],
"server" : "Community v5.2.0 (r135134-b14) schema 10 005",
"time" : "1 juin 2017 16:20:43"
}
I searched online, but I couldn't find any answers to this :/ Please if anyone have any idea ... thanks
So, I figured out how to upload a file :
var fs = require('fs')
var request = require('request')
var r = request.post('http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children?alf_ticket='+JSON.parse(chunk).data.ticket, function callback(err, httpResponse, body) {
if(err || JSON.parse(body).error) {
return console.log('Upload failed : ' + body)
}
console.log('Upload success')
})
var form = r.form()
form.append("name", "testFile.txt")
form.append("nodeType", "cm:content")
form.append("relativePath", "Sites/test-site/documentLibrary")
form.append("filedata",fs.createReadStream('./testFile.txt'))
Works fine to me =)