1
- import { Component , ElementRef , EventEmitter , Input , OnInit , Output , ViewChild } from '@angular/core' ;
2
- import { Headers } from '@angular/http' ;
1
+ import { Component , ElementRef , EventEmitter , Input , OnChanges , OnInit , Output , ViewChild } from '@angular/core' ;
2
+ import { Headers } from '@angular/http' ;
3
3
4
- import { ImageService } from './image.service' ;
5
- import { Style } from "./style" ;
6
- import { UploadMetadata } from './before-upload.interface' ;
4
+ import { ImageService } from './image.service' ;
5
+ import { Style } from "./style" ;
6
+ import { UploadMetadata } from './before-upload.interface' ;
7
7
8
8
export class FileHolder {
9
9
public pending : boolean = false ;
10
10
public serverResponse : { status : number , response : any } ;
11
11
12
- constructor ( public src : string , public file : File ) { }
12
+ constructor ( public src : string , public file : File ) {
13
+ }
13
14
}
14
15
15
16
@Component ( {
16
17
selector : 'image-upload' ,
17
18
templateUrl : './image-upload.component.html' ,
18
19
styleUrls : [ './image-upload.component.css' ]
19
20
} )
20
- export class ImageUploadComponent implements OnInit {
21
+ export class ImageUploadComponent implements OnInit , OnChanges {
21
22
22
23
files : FileHolder [ ] = [ ] ;
23
24
fileCounter : number = 0 ;
@@ -39,6 +40,7 @@ export class ImageUploadComponent implements OnInit {
39
40
@Input ( 'extensions' ) supportedExtensions : string [ ] ;
40
41
@Input ( ) url : string ;
41
42
@Input ( ) withCredentials : boolean = false ;
43
+ @Input ( ) uploadedFiles : string [ ] | Array < { url : string , fileName : string , blob ?: Blob } > = [ ] ;
42
44
@Output ( ) removed : EventEmitter < FileHolder > = new EventEmitter < FileHolder > ( ) ;
43
45
@Output ( ) uploadStateChanged : EventEmitter < boolean > = new EventEmitter < boolean > ( ) ;
44
46
@Output ( ) uploadFinished : EventEmitter < FileHolder > = new EventEmitter < FileHolder > ( ) ;
@@ -47,7 +49,8 @@ export class ImageUploadComponent implements OnInit {
47
49
private inputElement : ElementRef ;
48
50
private pendingFilesCounter : number = 0 ;
49
51
50
- constructor ( private imageService : ImageService ) { }
52
+ constructor ( private imageService : ImageService ) {
53
+ }
51
54
52
55
ngOnInit ( ) {
53
56
if ( ! this . fileTooLargeMessage ) {
@@ -71,6 +74,12 @@ export class ImageUploadComponent implements OnInit {
71
74
this . removed . emit ( file ) ;
72
75
}
73
76
77
+ ngOnChanges ( changes ) {
78
+ if ( changes . uploadedFiles && changes . uploadedFiles . currentValue . length > 0 ) {
79
+ this . processUploadedFiles ( ) ;
80
+ }
81
+ }
82
+
74
83
onFileChange ( files : FileList ) {
75
84
let remainingSlots = this . countRemainingSlots ( ) ;
76
85
let filesToUploadNum = files . length > remainingSlots ? remainingSlots : files . length ;
@@ -99,6 +108,28 @@ export class ImageUploadComponent implements OnInit {
99
108
}
100
109
}
101
110
111
+ private processUploadedFiles ( ) {
112
+ for ( let i = 0 ; i < this . uploadedFiles . length ; i ++ ) {
113
+ let data : any = this . uploadedFiles [ i ] ;
114
+
115
+ let fileBlob : Blob ,
116
+ file : File ,
117
+ fileUrl : string ;
118
+
119
+ if ( data instanceof Object ) {
120
+ fileUrl = data . url ;
121
+ fileBlob = ( data . blob ) ? data . blob : new Blob ( [ data ] ) ;
122
+ file = new File ( [ fileBlob ] , data . fileName ) ;
123
+ } else {
124
+ fileUrl = data ;
125
+ fileBlob = new Blob ( [ fileUrl ] ) ;
126
+ file = new File ( [ fileBlob ] , fileUrl ) ;
127
+ }
128
+
129
+ this . files . push ( new FileHolder ( fileUrl , file ) ) ;
130
+ }
131
+ }
132
+
102
133
private async uploadFiles ( files : FileList , filesToUploadNum : number ) {
103
134
for ( let i = 0 ; i < filesToUploadNum ; i ++ ) {
104
135
const file = files [ i ] ;
@@ -110,7 +141,7 @@ export class ImageUploadComponent implements OnInit {
110
141
continue ;
111
142
}
112
143
113
- const beforeUploadResult : UploadMetadata = await this . beforeUpload ( { file, url : this . url , abort : false } ) ;
144
+ const beforeUploadResult : UploadMetadata = await this . beforeUpload ( { file, url : this . url , abort : false } ) ;
114
145
115
146
if ( beforeUploadResult . abort ) {
116
147
this . fileCounter -- ;
@@ -139,11 +170,11 @@ export class ImageUploadComponent implements OnInit {
139
170
this . imageService
140
171
. postImage ( this . url , fileHolder . file , this . headers , this . partName , customForm , this . withCredentials )
141
172
. subscribe (
142
- response => this . onResponse ( response , fileHolder ) ,
143
- error => {
144
- this . onResponse ( error , fileHolder ) ;
145
- this . deleteFile ( fileHolder ) ;
146
- } ) ;
173
+ response => this . onResponse ( response , fileHolder ) ,
174
+ error => {
175
+ this . onResponse ( error , fileHolder ) ;
176
+ this . deleteFile ( fileHolder ) ;
177
+ } ) ;
147
178
} else {
148
179
this . uploadFinished . emit ( fileHolder ) ;
149
180
}
0 commit comments