通过JS API上传Alfresco中的文件

时间:2017-06-01 09:51:46

标签: javascript alfresco

我试图将文件从节点应用程序上传到我当地的Alfresco。 我设法登录,创建和删除文件夹,但没有文件。

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");
});

前面的代码返回错误:

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"}}

而且我不知道自己做错了什么,我尝试了各种方法,其中列出了不同的参数:https://www.npmjs.com/package/alfresco-js-api#upload-file但总是得到同样的错误...... 如果有人能帮助我,那就太好了,谢谢=)

编辑: 所以我放弃了这个方法,并开始直接尝试休息请求,我设法编写了这段代码:

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();

但是,现在,我有一个错误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"
}

我在线搜索,但我找不到任何答案:/ 如果有人有任何想法,请...谢谢

3 个答案:

答案 0 :(得分:0)

基本上import { Component } from '@angular/core'; import { AlfrescoApi } from 'alfresco-js-api'; @Component({ selector: 'home-view', templateUrl: './home.component.html', styleUrls: ['./home.component.css'] }) export class HomeComponent { public file: File; changeListener(event) { this.file = event.target.files[0]; } uploadFileFromUI() { let fs = require('fs'); let alfrescoApi = require('alfresco-js-api'); let alfrescoJsApi = new alfrescoApi(); let fileToUpload = this.file; alfrescoJsApi.login('admin', 'admin').then(function (data) { console.log('API called successfully login ticket:' + data); 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'); }); } } 行存在一些问题。我刚刚在下面的链接上找到了它。

https://github.com/angular/angular-cli/issues/5324

下面是文件上传的工作示例,使用html文件输入元素。我没有覆盖太多东西。基本上在默认的露天角度组件上我只使用了家庭组件并在其中添加了代码并修改了所需的东西比如添加一个html文件上传元素。

<强> home.component.ts

<!-- DOCUMENT LIST-->
<div class="demo-card-square mdl-card mdl-shadow--2dp home-cards">
  <div class="mdl-card__title mdl-card--expand" routerLink="/files">
    <h2 class="mdl-card__title-text">
      <i class="material-icons home--card__icon">dvr</i>
                <span class="home--card__text">DocumentList - Content Services</span>
    </h2>
  </div>
  <div class="mdl-card__supporting-text">
    Demonstrates multiple Alfresco Content Services components used together to display the files of your Content Services instance:
    <ul class="home--feature-list">
      <li>
        <i class="material-icons home--feature-list__icon">brightness_1</i>
        <span class="home--feature-list__text">Communication with the Rest Api and core services</span>
        <a href="https://www.npmjs.com/package/ng2-alfresco-core" target="_blank">ng2-alfresco-core</a>
      </li>
      <li>
        <i class="material-icons home--feature-list__icon">dvr</i>
        <span class="home--feature-list__text">Document List</span>
        <a href="https://www.npmjs.com/package/ng2-alfresco-documentlist" target="_blank">ng2-alfresco-documentlist</a>
      </li>
      <li>
        <i class="material-icons home--feature-list__icon">file_upload</i>
        <span class="home--feature-list__text">Upload</span>
        <a href="https://www.npmjs.com/package/ng2-alfresco-upload" target="_blank">ng2-alfresco-upload</a>
      </li>
      <li>
        <i class="material-icons home--feature-list__icon">view_module</i>
        <span class="home--feature-list__text">DataTable</span>
        <a href="https://www.npmjs.com/package/ng2-alfresco-datatable" target="_blank">ng2-alfresco-datatable</a>
      </li>
    </ul>
  </div>
</div>


<!-- Process Services-->
<div class="demo-card-square mdl-card mdl-shadow--2dp home-cards">
  <div class="mdl-card__title mdl-card--expand" routerLink="/activiti">
    <h2 class="mdl-card__title-text">
      <i class="material-icons home--card__icon">apps</i>
                <span class="home--card__text">Process Services</span>
    </h2>
  </div>
  <div class="mdl-card__supporting-text">
    Demonstrates multiple Alfresco Process Services components used together to show your Process Services process and tasks:
    <ul class="home--feature-list">
      <li>
        <i class="material-icons home--feature-list__icon">brightness_1</i>
        <span class="home--feature-list__text">Communication with the Rest Api and core services</span>
        <a href="https://www.npmjs.com/package/ng2-alfresco-core" target="_blank">ng2-alfresco-core</a>
      </li>
      <li>
        <i class="material-icons home--feature-list__icon">view_module</i>
        <span class="home--feature-list__text">App List</span>
        <a href="https://www.npmjs.com/package/ng2-activiti-tasklist" target="_blank">ng2-activiti-apps</a>
      </li>
      <li>
        <i class="material-icons home--feature-list__icon">view_headline</i>
        <span class="home--feature-list__text">Task List</span>
        <a href="https://www.npmjs.com/package/ng2-activiti-tasklist" target="_blank">ng2-activiti-tasklist</a>
      </li>
      <li>
        <i class="material-icons home--feature-list__icon">view_headline</i>
        <span class="home--feature-list__text">Process List</span>
        <a href="https://www.npmjs.com/package/ng2-activiti-processlist" target="_blank">ng2-activiti-processlist</a>
      </li>
      <li>
        <i class="material-icons home--feature-list__icon">view_quilt</i>
        <span class="home--feature-list__text">Form</span>
        <a href="https://www.npmjs.com/package/ng2-activiti-form" target="_blank">ng2-activiti-form</a>
      </li>
      <li>
        <i class="material-icons home--feature-list__icon">pie_chart</i>
        <span class="home--feature-list__text">Analytics</span>
        <a href="https://www.npmjs.com/package/ng2-activiti-analytics" target="_blank">ng2-activiti-analytics</a>,
        <a href="https://www.npmjs.com/package/ng2-activiti-diagrams" target="_blank">ng2-activiti-diagrams</a>
      </li>
      <li>
        <i class="material-icons home--feature-list__icon">view_module</i>
        <span class="home--feature-list__text">DataTable</span>
        <a href="https://www.npmjs.com/package/ng2-alfresco-datatable" target="_blank">ng2-alfresco-datatable</a>
      </li>
    </ul>
  </div>
</div>


<!-- DATATABLE-->
<div class="demo-card-square mdl-card mdl-shadow--2dp home-cards">
  <div class="mdl-card__title mdl-card--expand" routerLink="/datatable">
    <h2 class="mdl-card__title-text">
      <i class="material-icons home--card__icon">view_module</i>
                <span class="home--card__text">DataTable - Content Services & Process Services</span>
    </h2>
  </div>
  <div class="mdl-card__supporting-text">
    Basic table component:
    <ul class="home--feature-list">
      <li>
        <i class="material-icons home--feature-list__icon">brightness_1</i>
        <span class="home--feature-list__text">Communication with the Rest Api and core services</span>
        <a href="https://www.npmjs.com/package/ng2-alfresco-core" target="_blank">ng2-alfresco-core</a>
      </li>
    </ul>
  </div>
</div>

<!-- UPLOADER-->
<div class="demo-card-square mdl-card mdl-shadow--2dp home-cards">
  <div class="mdl-card__title mdl-card--expand" routerLink="/uploader">
    <h2 class="mdl-card__title-text">
      <i class="material-icons home--card__icon">file_upload</i>
                <span class="home--card__text">Uploader - Content Services</span>
    </h2>
  </div>
  <div class="mdl-card__supporting-text">
    Basic table uploader component for the Content Services & Process Services:
    <ul class="home--feature-list">
      <li>
        <i class="material-icons home--feature-list__icon">brightness_1</i>
        <span class="home--feature-list__text">Communication with the Rest Api and core services</span>
        <a href="https://www.npmjs.com/package/ng2-alfresco-core" target="_blank">ng2-alfresco-core</a>
      </li>
    </ul>
  </div>
</div>

<!-- LOGIN-->
<div class="demo-card-square mdl-card mdl-shadow--2dp home-cards">
  <div class="mdl-card__title mdl-card--expand" routerLink="/login">
    <h2 class="mdl-card__title-text">
      <i class="material-icons home--card__icon">account_circle</i>
                <span class="home--card__text">Login - Content Services & Process Services</span>
    </h2>
  </div>
  <div class="mdl-card__supporting-text">
        Login component for the Content Services and Process Services:
    <ul class="home--feature-list">
      <li>
        <i class="material-icons home--feature-list__icon">brightness_1</i>
        <span class="home--feature-list__text">Communication with the Rest Api and core services</span>
        <a href="https://www.npmjs.com/package/ng2-alfresco-core" target="_blank">ng2-alfresco-core</a>
      </li>
    </ul>
  </div>
</div>

<!-- WEBSCRIPT-->
<div class="demo-card-square mdl-card mdl-shadow--2dp home-cards">
  <div class="mdl-card__title mdl-card--expand" routerLink="/webscript">
    <h2 class="mdl-card__title-text">
      <i class="material-icons home--card__icon">extension</i>
            <span class="home--card__text">Webscript - Content Services</span>
    </h2>
  </div>
  <div class="mdl-card__supporting-text">
    Displays and creates webscripts in your Content Services instance:
    <ul class="home--feature-list">
      <li>
        <i class="material-icons home--feature-list__icon">brightness_1</i>
        <span class="home--feature-list__text">Communication with the Rest Api and core services</span>
        <a href="https://www.npmjs.com/package/ng2-alfresco-core" target="_blank">ng2-alfresco-core</a>
      </li>
    </ul>
  </div>
</div>

<!-- TAG-->
<div class="demo-card-square mdl-card mdl-shadow--2dp home-cards">
  <div class="mdl-card__title mdl-card--expand" routerLink="/tag">
    <h2 class="mdl-card__title-text">
      <i class="material-icons home--card__icon">local_offer</i>
                <span class="home--card__text">Tag - Content Services</span>
    </h2>
  </div>
  <div class="mdl-card__supporting-text">
    Displays and adds tags to the node of your Content Services instance:
    <ul class="home--feature-list">
      <li>
        <i class="material-icons home--feature-list__icon">brightness_1</i>
        <span class="home--feature-list__text">Communication with Rest</span>
        <a href="https://www.npmjs.com/package/ng2-alfresco-core" target="_blank">ng2-alfresco-core</a>
      </li>
    </ul>
  </div>
</div>
<br/>
<br/>
<br/>
 <input type="file" (change)="changeListener($event)">
<button (click)='uploadFileFromUI()'>upload</button>

<强> home.component.html

JSONObject obj = new JSONObject("your json element");
JSONObject obj2 = (JSONObject) obj.get("76800769");

System.out.println(obj2.get("brojCelija"));
System.out.println(obj2.get("prosjekLat"));
System.out.println(obj2.get("prosjekLong"));

答案 1 :(得分:0)

所以,我想出了如何上传文件:

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'))

对我来说工作正常=)

答案 2 :(得分:0)

alfresco-js-api可以正常工作,但是您在第一个代码中选择了错误的模块。 您必须选择 alfresco-js-api-node 而不是 alfresco-js-api

浏览器版本的安装程序:

npm install --save alfresco-js-api

节点版本的安装程序:

npm install --save alfresco-js-api-node

用于节点项目的导入库

var AlfrescoApi = require('alfresco-js-api-node');