I have 2 CKEditor fields () in the View.
<div id="editor_1">
@Html.Raw(@Model.Description_1)
</div>
<div id="editor_2">
@Html.Raw(@Model.Description_2)
</div>
There is a code that transmits the uploaded image to the controller:
<script>
class MyUploadAdapter {
upload() {
return this.loader.file
.then( file => new Promise( ( resolve, reject ) => {
this._initRequest();
this._initListeners( resolve, reject, file );
this._sendRequest( file );
} ) );
}
_initRequest() {
const xhr = this.xhr = new XMLHttpRequest();
}
}
</script>
How do I pass in _initRequest() a link to the elements and to understand which field the user is uploading the image to (I need to get the field id)? I tried to figure it out in the controller (in Request class) where I receive the uploaded image, but I couldn’t.
foreach (IFormFile photo in Request.Form.Files)
Thanks!
2
Answers
I solved the problem by myself by getting the focus - in which field the image is uploaded. The issue is closed. https://ckeditor.com/docs/ckeditor5/latest/framework/deep-dive/ui/focus-tracking.html
You can create a custom upload adapter to send files to the backend as part of the request. When creating an instance of the adapter, you can get the id of the current element through
editor.sourceElement.getAttribute('id')
.Then, create a new XMLHttpRequest and add the element id to the request headerFinally, it is sent to the backend via HTTP request headers.
On the backend, a method is used to obtain the corresponding id from the request header.
Here is an example you can use as a reference:
accept method:
When I send the image:
I can get the corresponding element id in the request header: