簡體   English   中英

通過 JS API 在 Alfresco 上傳文件

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

前面的代碼返回錯誤:

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

我在網上搜索,但我找不到任何答案:/請如果有人有任何想法......謝謝

基本上有一些問題let fs = require('fs'); 行。我剛在下面的鏈接上得到它。

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

下面是文件上傳的工作示例,使用 html 文件輸入元素。我沒有覆蓋太多東西。基本上在默認的 alfresco angular 組件上,我只是使用了 home 組件並在其中添加了您的代碼並修改了所需的內容,例如添加 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');
          });
   }
}

主頁.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');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM