I have prepared an admin form containing a file upload field. This form is configured in an .xml form file.
<field name="files" formElement="fileUploader" sortOrder="40">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">string</item>
<item name="label" xsi:type="string" translate="true">Files</item>
<item name="notice" xsi:type="string">Maximum file size: 50MB. Allowed file types: PDF.</item>
<item name="formElement" xsi:type="string">fileUploader</item>
<item name="isMultipleFiles" xsi:type="boolean">true</item>
<item name="allowedExtensions" xsi:type="string">pdf</item>
<item name="maxFileSize" xsi:type="number">52428800</item>
<item name="placeholderType" xsi:type="string">document</item>
<item name="uploaderConfig" xsi:type="array">
<item name="url" xsi:type="url" path="my_custom/route/upload"/>
</item>
</item>
</argument>
</field>
When I click upload and select a file, the file is uploaded and saved to the server and it works fine. Responsible for this is the route set in the .xml file.
How can I delete a file from the server when I click the trash button in the lower left corner of the file icon? Now clicking the trash button removes the file from the form but not from the server, so I have to submit the form to remove the file from the server, but what if the user opens a new form and submits the file and then leaves the form without saving. The uploaded file will still exist on the server. I thought of sending a request when the trash icon is clicked to a specific route that will be responsible for deleting the file eg. my_custom/route/deletefile. Do you have any idea how I can achieve this? I am using Magento version 2.4.4.