I am facing an issue while attempting to upload a file from a node application to my local Alfresco server. I have been able to login, create, and delete folders successfully, but I am encountering difficulties when trying to upload files.
let AlfrescoApi = require('alfresco-js-api');
let alfrescoJsApi = new AlfrescoApi();
let fs = require('fs');
alfrescoJsApi.login('admin', 'admin').then(function (data) {
console.log('Successfully logged in, received 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 above code is throwing the following error:
Error during the uploadError: {"error":{"errorKey":"Required parameters are missing","statusCode":400,"briefSummary":"05010132 Required parameters are missing","stackTrace":"For security reasons, stack trace display has been disabled.","descriptionURL":"https://api-explorer.alfresco.com"}}
I have tried various methods with different parameters as listed here: https://www.npmjs.com/package/alfresco-js-api#upload-file but keep getting the same error. Any help would be greatly appreciated. Thank you =)
EDIT : After struggling with the previous method, I decided to explore using REST requests directly. Here is the code snippet I came up with:
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();
However, now I am receiving 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 have searched online for solutions but couldn't find any. If anyone has any insights, I would greatly appreciate your help. Thank you.