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' ;
33
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' ;
77
88export class FileHolder {
99 public pending : boolean = false ;
1010 public serverResponse : { status : number , response : any } ;
1111
12- constructor ( public src : string , public file : File ) { }
12+ constructor ( public src : string , public file : File ) {
13+ }
1314}
1415
1516@Component ( {
1617 selector : 'image-upload' ,
1718 templateUrl : './image-upload.component.html' ,
1819 styleUrls : [ './image-upload.component.css' ]
1920} )
20- export class ImageUploadComponent implements OnInit {
21+ export class ImageUploadComponent implements OnInit , OnChanges {
2122
2223 files : FileHolder [ ] = [ ] ;
2324 fileCounter : number = 0 ;
@@ -39,6 +40,7 @@ export class ImageUploadComponent implements OnInit {
3940 @Input ( 'extensions' ) supportedExtensions : string [ ] ;
4041 @Input ( ) url : string ;
4142 @Input ( ) withCredentials : boolean = false ;
43+ @Input ( ) uploadedFiles : string [ ] | Array < { url : string , fileName : string , blob ?: Blob } > = [ ] ;
4244 @Output ( ) removed : EventEmitter < FileHolder > = new EventEmitter < FileHolder > ( ) ;
4345 @Output ( ) uploadStateChanged : EventEmitter < boolean > = new EventEmitter < boolean > ( ) ;
4446 @Output ( ) uploadFinished : EventEmitter < FileHolder > = new EventEmitter < FileHolder > ( ) ;
@@ -47,7 +49,8 @@ export class ImageUploadComponent implements OnInit {
4749 private inputElement : ElementRef ;
4850 private pendingFilesCounter : number = 0 ;
4951
50- constructor ( private imageService : ImageService ) { }
52+ constructor ( private imageService : ImageService ) {
53+ }
5154
5255 ngOnInit ( ) {
5356 if ( ! this . fileTooLargeMessage ) {
@@ -71,6 +74,12 @@ export class ImageUploadComponent implements OnInit {
7174 this . removed . emit ( file ) ;
7275 }
7376
77+ ngOnChanges ( changes ) {
78+ if ( changes . uploadedFiles && changes . uploadedFiles . currentValue . length > 0 ) {
79+ this . processUploadedFiles ( ) ;
80+ }
81+ }
82+
7483 onFileChange ( files : FileList ) {
7584 let remainingSlots = this . countRemainingSlots ( ) ;
7685 let filesToUploadNum = files . length > remainingSlots ? remainingSlots : files . length ;
@@ -99,6 +108,28 @@ export class ImageUploadComponent implements OnInit {
99108 }
100109 }
101110
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+
102133 private async uploadFiles ( files : FileList , filesToUploadNum : number ) {
103134 for ( let i = 0 ; i < filesToUploadNum ; i ++ ) {
104135 const file = files [ i ] ;
@@ -110,7 +141,7 @@ export class ImageUploadComponent implements OnInit {
110141 continue ;
111142 }
112143
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 } ) ;
114145
115146 if ( beforeUploadResult . abort ) {
116147 this . fileCounter -- ;
@@ -139,11 +170,11 @@ export class ImageUploadComponent implements OnInit {
139170 this . imageService
140171 . postImage ( this . url , fileHolder . file , this . headers , this . partName , customForm , this . withCredentials )
141172 . 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+ } ) ;
147178 } else {
148179 this . uploadFinished . emit ( fileHolder ) ;
149180 }
0 commit comments