Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload multiple file to different url's using ng2-file-upload

I am having trouble uploading multiple file to different urls using ng2-file-upload method.

I am using this repo https://github.com/valor-software/ng2-file-upload to make this. ng2-file-upload is easily integrated in my app using this demo to upload files. Also, they provided a method to upload multiple files on a particular url.

But I want to upload different files at different url, which I am not able to find out yet.

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

This FileUploader provides way to give url to upload file. Usefulness of this method is that, progress-bar and related feature are integrated with this ng2-file-uploader component. I have searched a lot but not able to find any solution to upload to different urls.

Any useful suggestion will be appreciated!!

like image 553
yugantar kumar Avatar asked Aug 25 '17 13:08

yugantar kumar


1 Answers

Here is how i was able to do the same. You would have to override onBeforeUploadItem of the uploader.

define:

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

in the constructor

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

  return {fileItem};
};
like image 102
Sabu George Avatar answered Oct 12 '22 11:10

Sabu George