通过 JS 在 Alfresco 中上传文件 API

Uploading a file in Alfresco via JS API

我正在尝试将文件从节点应用程序上传到我本地的 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");
});

之前的代码return错误:

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 但总是出现相同的错误... 如果有人能帮助我那就太好了,谢谢 =)


编辑: 所以我放弃了这个方法,开始尝试直接用rest requests,我成功写了这段代码:

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

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

基本上 let fs = require('fs'); 有一些问题 line.I 刚刚在下面 link.

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

下面是文件上传的工作示例,使用 html 文件输入 element.I 并没有覆盖太多 thing.Basically 我刚刚在家使用的默认露天 angular 组件组件并在其中添加您的代码并修改所需的内容,例如添加 html 文件上传元素。

home.component.ts

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

home.component.html

<!-- 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>

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

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

对我来说很好 =)

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