Browse Source

Merge pull request #282 from ckeditor/t/ckeditor5/1618

Docs: Aligned the "Custom image upload adapter" guide to recent changes in `FileLoader#file` API. Closes ckeditor/ckeditor5#1618. [skip ci]
Aleksander Nowodzinski 6 years ago
parent
commit
c2ef08e995

+ 36 - 26
packages/ckeditor5-image/docs/framework/guides/deep-dive/upload-adapter.md

@@ -185,7 +185,7 @@ class MyUploadAdapter {
 		return new Promise( ( resolve, reject ) => {
 			this._initRequest();
 			this._initListeners( resolve, reject );
-			this._sendRequest();
+			this._sendRequest( reject );
 		} );
 	}
 
@@ -292,18 +292,23 @@ class MyUploadAdapter {
 	// ...
 
 	// Prepares the data and sends the request.
-	_sendRequest() {
-		// Prepare the form data.
-		const data = new FormData();
-		data.append( 'upload', this.loader.file );
-
-		// Important note: This is the right place to implement security mechanisms
-		// like authentication and CSRF protection. For instance, you can use
-		// XMLHttpRequest.setRequestHeader() to set the request headers containing
-		// the CSRF token generated earlier by your application.
-
-		// Send the request.
-		this.xhr.send( data );
+	_sendRequest( reject ) {
+		this.loader.file
+			.then( file => {
+				// Prepare the form data.
+				const data = new FormData();
+
+				data.append( 'upload', file );
+
+				// Important note: This is the right place to implement security mechanisms
+				// like authentication and CSRF protection. For instance, you can use
+				// XMLHttpRequest.setRequestHeader() to set the request headers containing
+				// the CSRF token generated earlier by your application.
+
+				// Send the request.
+				this.xhr.send( data );
+			} )
+			.catch( reject );
 	}
 }
 ```
@@ -414,7 +419,7 @@ class MyUploadAdapter {
 		return new Promise( ( resolve, reject ) => {
 			this._initRequest();
 			this._initListeners( resolve, reject );
-			this._sendRequest();
+			this._sendRequest( reject );
 		} );
 	}
 
@@ -481,18 +486,23 @@ class MyUploadAdapter {
 	}
 
 	// Prepares the data and sends the request.
-	_sendRequest() {
-		// Prepare the form data.
-		const data = new FormData();
-		data.append( 'upload', this.loader.file );
-
-		// Important note: This is the right place to implement security mechanisms
-		// like authentication and CSRF protection. For instance, you can use
-		// XMLHttpRequest.setRequestHeader() to set the request headers containing
-		// the CSRF token generated earlier by your application.
-
-		// Send the request.
-		this.xhr.send( data );
+	_sendRequest( reject ) {
+		this.loader.file
+			.then( file => {
+				// Prepare the form data.
+				const data = new FormData();
+
+				data.append( 'upload', file );
+
+				// Important note: This is the right place to implement security mechanisms
+				// like authentication and CSRF protection. For instance, you can use
+				// XMLHttpRequest.setRequestHeader() to set the request headers containing
+				// the CSRF token generated earlier by your application.
+
+				// Send the request.
+				this.xhr.send( data );
+			} )
+			.catch( reject );
 	}
 }