cancel
Showing results for 
Search instead for 
Did you mean: 

Uploading a file through JS API

alexandreb
Confirmed Champ
Confirmed Champ

Hi,
I'm trying to use the https://www.npmjs.com/package/alfresco-js-api  to build an application (Node) aiming to create files in Alfresco.

I have a Community Edition Alfresco installed.

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);
    }, function (error) {
        console.log("Error, cannot connect to Alfresco");
    });

var fileToUpload = fs.createReadStream('./testFile.txt');

alfrescoJsApi.upload.uploadFile(fileToUpload)
        .then(function () {
            console.log('File Uploaded');
        }, function (error) {
            console.log('Error during the upload' + error);
        });

Previous code doesn't work and I don't know why, I can create a folder with  "  alfrescoJsApi.nodes.createFolder();  " but cannot manage to create a file.
In log I'm obtaining following error :

Error during the uploadError: {"error":{"errorKey":"Required parameters are missing","statusCode":400,"briefSummary":"04310053 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"}}

If anyone have any idea of what I'm doing wrong 😕

Thanks in advance

1 ACCEPTED ANSWER

alexandreb
Confirmed Champ
Confirmed Champ

Okay so I figured out how to upload a File, once logged and var ticket initialized :

var request = require('request')
var fs = require('fs')

var r = request.post('http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children?alf_ti..., 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'))

I don't use alfresco-js-api anymore

View answer in original post

15 REPLIES 15

mehe
Elite Collaborator
Elite Collaborator

...hmmm, your alfresco and alfresco-js-api version is?

Alfresco Community v5.2.0

"alfresco-js-api": "^1.5.0"

Okay, I managed to create a file using REST, doing few changes :

In var options :

'headers': {
        'Content-Type': 'application/json'
        }

Also, the path to upload :

options.path = '/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children?alf_ticket='+JSON.parse(chunk).data.ticket

But I still don't know how to upload an existing file

alexandreb
Confirmed Champ
Confirmed Champ

Okay so I figured out how to upload a File, once logged and var ticket initialized :

var request = require('request')
var fs = require('fs')

var r = request.post('http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-root-/children?alf_ti..., 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'))

I don't use alfresco-js-api anymore

mehe
Elite Collaborator
Elite Collaborator

Hi Alexandre,

sorry I had no time to answer - I'm using alfresco-js-api with angularJS clients but didn't use the upload function until now (migrating from old stuff, upload is still the old code).

I had some login issues with alfresco-js-api, but after asking under the adf section of alfresco community, my problems were solved in no time.

If you are interested, I will share my experience when using (or trying to use) the upload-function - but this won't be before July.

It would be helpful if you could share your experience in July.