Skip to content

Commit

Permalink
feat(multipart): Create disableMultipart option in FileUploader (#224)
Browse files Browse the repository at this point in the history
* Create disableMultipart option in FileUploader

* Add basic docs
  • Loading branch information
tdonohue authored and valorkin committed Jun 21, 2016
1 parent e068511 commit 22307d2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ Follow me [![twitter](https://img.shields.io/twitter/follow/valorkin.svg?style=s

- `uploader` - (`FileUploader`) - uploader object. See using in [demo](https://github.com/valor-software/ng2-file-upload/blob/master/demo/components/file-upload/simple-demo.ts)

Parameters that supported by this object:
Parameters supported by this object:

1. `url` - URL of File Uploader's route
2. `authToken` - Auth token that will be applied as 'Authorization' header during file send.
3. `disableMultipart` - If 'true', disable using a multipart form for file upload and instead stream the file. Some APIs (e.g. Amazon S3) may expect the file to be streamed rather than sent via a form. Defaults to false.

### Events

Expand Down
18 changes: 13 additions & 5 deletions components/file-upload/file-uploader.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface FileUploaderOptions {
queueLimit?:number;
removeAfterUpload?:boolean;
url?:string;
disableMultipart?:boolean;
}

export class FileUploader {
Expand All @@ -43,7 +44,8 @@ export class FileUploader {
autoUpload: false,
isHTML5: true,
filters: [],
removeAfterUpload: false
removeAfterUpload: false,
disableMultipart: false
};

private _failFilterIndex:number;
Expand Down Expand Up @@ -282,7 +284,7 @@ export class FileUploader {

protected _xhrTransport(item:FileItem):any {
let xhr = item._xhr = new XMLHttpRequest();
let form = new FormData();
let sendable:any;
this._onBeforeUploadItem(item);
// todo
/*item.formData.map(obj => {
Expand All @@ -293,9 +295,15 @@ export class FileUploader {
if (typeof item._file.size !== 'number') {
throw new TypeError('The file specified is no longer valid');
}
this._onBuildItemForm(item, form);
if(!this.options.disableMultipart) {
sendable = new FormData();
this._onBuildItemForm(item, sendable);

sendable.append(item.alias, item._file, item.file.name);
} else {
sendable = item._file;
}

form.append(item.alias, item._file, item.file.name);
xhr.upload.onprogress = (event:any) => {
let progress = Math.round(event.lengthComputable ? event.loaded * 100 / event.total : 0);
this._onProgressItem(item, progress);
Expand Down Expand Up @@ -334,7 +342,7 @@ export class FileUploader {
if (this.authToken) {
xhr.setRequestHeader('Authorization', this.authToken);
}
xhr.send(form);
xhr.send(sendable);
this._render();
}

Expand Down
3 changes: 2 additions & 1 deletion components/file-upload/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import {FileSelectDirective, FileDropDirective, FileUploader} from 'ng2-file-upl

- `uploader` - (`FileUploader`) - uploader object. See using in [demo](https://github.com/valor-software/ng2-file-upload/blob/master/demo/components/file-upload/simple-demo.ts)

Parameters that supported by this object:
Parameters supported by this object:

1. `url` - URL of File Uploader's route
2. `authToken` - auth token that will be applied as 'Authorization' header during file send.
3. `disableMultipart` - If 'true', disable using a multipart form for file upload and instead stream the file. Some APIs (e.g. Amazon S3) may expect the file to be streamed rather than sent via a form. Defaults to false.

## FileDrop API

Expand Down

0 comments on commit 22307d2

Please sign in to comment.