首页 文章

使用ng2-file-upload将多个文件上传到不同的URL

提问于
浏览
8

我遇到了麻烦 uploading multiple file to different urls using ng2-file-upload method .

我正在使用这个回购https://github.com/valor-software/ng2-file-upload来做这件事 . ng2-file-upload 可以使用此demo轻松集成到我的应用程序中来上传文件 . 此外,他们提供了一个method来上传特定网址上的多个文件 .

但我想上传 different files at different url ,我还没知道 .

// sample code
import { Component } from '@angular/core';
import { FileUploader } from 'ng2-file-upload';

const URL = '/api/some_url';

@Component({
  selector: 'simple-demo',
  templateUrl: './simple-demo.html'
})
export class SimpleDemoComponent {
  public uploader:FileUploader = new FileUploader({url: URL}); // here to add url
}

这个 FileUploader 提供了 url 上传文件的方法 . 此方法的有用之处在于, progress-bar 和相关功能与此 ng2-file-uploader 组件集成在一起 . 我搜索了很多,但无法找到任何上传到不同网址的解决方案 .

任何有用的建议将不胜感激!

1 回答

  • 5

    这是我如何能够做到这一点 . 您必须覆盖上传者的onBeforeUploadItem .

    限定:

    this.uploader = new FileUploader({url: this.uploadUrl});
    

    在构造函数中

    this.uploader.onBeforeUploadItem = (fileItem: FileItem): any => {
      // logic of connecting url with the file
      fileItem.url = 'http://localhost:3001/path/v1';
    
      return {fileItem};
    };
    

相关问题