@@ -27,11 +27,16 @@ export class PhotoUploadComponent implements OnInit {
2727 }
2828
2929 ngOnInit ( ) : void {
30+ // Create the file uploader, wire it to upload to your account
3031 const uploaderOptions : FileUploaderOptions = {
3132 url : `https://api.cloudinary.com/v1_1/${ this . cloudinary . config ( ) . cloud_name } /upload` ,
33+ // Upload files automatically upon addition to upload queue
3234 autoUpload : true ,
35+ // Use xhrTransport in favor of iframeTransport
3336 isHTML5 : true ,
37+ // Calculate progress independently for each uploaded file
3438 removeAfterUpload : true ,
39+ // XHR request headers
3540 headers : [
3641 {
3742 name : 'X-Requested-With' ,
@@ -41,9 +46,10 @@ export class PhotoUploadComponent implements OnInit {
4146 } ;
4247 this . uploader = new FileUploader ( uploaderOptions ) ;
4348
44- // Add custom tag for displaying the uploaded photo in the list
4549 this . uploader . onBuildItemForm = ( fileItem : any , form : FormData ) : any => {
50+ // Add Cloudinary's unsigned upload preset to the upload form
4651 form . append ( 'upload_preset' , this . cloudinary . config ( ) . upload_preset ) ;
52+ // Add built-in and custom tags for displaying the uploaded photo in the list
4753 let tags = 'myphotoalbum' ;
4854 if ( this . title ) {
4955 form . append ( 'context' , `photo=${ this . title } ` ) ;
@@ -52,6 +58,7 @@ export class PhotoUploadComponent implements OnInit {
5258 form . append ( 'tags' , tags ) ;
5359 form . append ( 'file' , fileItem ) ;
5460
61+ // Use default "withCredentials" value for CORS requests
5562 fileItem . withCredentials = false ;
5663 return { fileItem, form } ;
5764 } ;
@@ -82,6 +89,7 @@ export class PhotoUploadComponent implements OnInit {
8289 } ) ;
8390 } ;
8491
92+ // Update model on completion of uploading a file
8593 this . uploader . onCompleteItem = ( item : any , response : string , status : number , headers : ParsedResponseHeaders ) =>
8694 upsertResponse (
8795 {
@@ -91,6 +99,7 @@ export class PhotoUploadComponent implements OnInit {
9199 }
92100 ) ;
93101
102+ // Update model on upload progress event
94103 this . uploader . onProgressItem = ( fileItem : any , progress : any ) =>
95104 upsertResponse (
96105 {
@@ -105,6 +114,9 @@ export class PhotoUploadComponent implements OnInit {
105114 this . title = value ;
106115 }
107116
117+ // Delete an uploaded image
118+ // Requires setting "Return delete token" to "Yes" in your upload preset configuration
119+ // See also https://support.cloudinary.com/hc/en-us/articles/202521132-How-to-delete-an-image-from-the-client-side-
108120 deleteImage = function ( data : any , index : number ) {
109121 const url = `https://api.cloudinary.com/v1_1/${ this . cloudinary . config ( ) . cloud_name } /delete_by_token` ;
110122 let headers = new Headers ( { 'Content-Type' : 'application/json' , 'X-Requested-With' : 'XMLHttpRequest' } ) ;
0 commit comments