Skip to content

Allow dynamic API endpoints #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "library-ws",
"name": "angular-file-uploader",
"version": "0.1.0",
"scripts": {
"ng": "ng",
Expand Down
47 changes: 47 additions & 0 deletions projects/angular-file-uploader/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,53 @@ Support this package if it really helped you, send your support at [Patreon](htt
}
};
```
##### Example-3 ( Dynamic target URL )
```html
<angular-file-uploader
[config]="afuConfig"
[resetUpload]="resetVar"
(ApiResponse)="UploadDone($event)">
</angular-file-uploader>
```
```javascript
afuConfig = {
multiple: false,
formatsAllowed: ".jpg,.png",
maxSize: "1",
uploadAPI: {
url: () => function() {
// Can be promise or not, but anyway: We'll await for it
return new Promise(function(resolve) {
resolve("https://example-file-upload-api");
});
},
method:"POST",
headers: {
"Content-Type" : "text/plain;charset=UTF-8",
"Authorization" : `Bearer ${token}`
},
params: {
'page': '1'
},
responseType: 'blob',
},
theme: "dragNDrop",
hideProgressBar: true,
hideResetBtn: true,
hideSelectBtn: true,
fileNameIndex: true,
replaceTexts: {
selectFileBtn: 'Select Files',
resetBtn: 'Reset',
uploadBtn: 'Upload',
dragNDropBox: 'Drag N Drop',
attachPinBtn: 'Attach Files...',
afterUploadMsg_success: 'Successfully Uploaded !',
afterUploadMsg_error: 'Upload Failed !',
sizeLimit: 'Size Limit'
}
};
```

| **Properties** | **Description** | **Default Value** |
|----------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@ import {
EventEmitter,
Input,
OnChanges,
OnInit,
Output,
SimpleChanges,
} from '@angular/core';
import {
ReplaceTexts,
AngularFileUploaderConfig,
UploadInfo,
UploadApi,
UploadInfo
} from './angular-file-uploader.types';
import {
HttpClient,
HttpHeaders,
HttpParams,
HttpEventType,
} from '@angular/common/http';
import { map } from 'rxjs/operators';

@Component({
selector: 'angular-file-uploader',
Expand All @@ -46,7 +43,7 @@ export class AngularFileUploaderComponent implements OnChanges {
id: number;
hideProgressBar: boolean;
maxSize: number;
uploadAPI: string;
uploadAPI: string|((p: this, formData: FormData, options: { headers: any; params: any }) => Promise<string>|string);
method: string;
formatsAllowed: string;
multiple: boolean;
Expand Down Expand Up @@ -193,7 +190,7 @@ export class AngularFileUploaderComponent implements OnChanges {
event.target.value = null;
}

uploadFiles() {
async uploadFiles() {
this.progressBarShow = true;
this.uploadStarted = true;
this.notAllowedFiles = [];
Expand Down Expand Up @@ -229,12 +226,16 @@ export class AngularFileUploaderComponent implements OnChanges {
if (this.responseType) (options as any).responseType = this.responseType;

this.http
.request(this.method.toUpperCase(), this.uploadAPI, {
body: formData,
reportProgress: true,
observe: 'events',
...options,
})
.request(
this.method.toUpperCase(),
typeof this.uploadAPI === 'function' ? await this.uploadAPI(this, formData, options) : this.uploadAPI,
{
body: formData,
reportProgress: true,
observe: 'events',
...options,
}
)
.subscribe(
(event) => {
// Upload Progress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface ReplaceTexts {
}

export interface UploadApi {
url: string;
url: string|((p: any, formData: FormData, options: { headers: any; params: any }) => Promise<string>|string);
method?: 'POST' | 'PUT' | 'PATCH';
headers?: HttpHeaders | { [header: string]: string | string[] };
params?: HttpParams | { [param: string]: string | string[]; };
Expand Down