This is a migrated thread and some comments may be shown as answers.

Upload control: how to reject file via custom saveUrl?

2 Answers 297 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Ruud
Top achievements
Rank 1
Ruud asked on 08 Nov 2019, 03:25 PM

Hi,

I'm creating a file upload control. I need to use a custom endpoint for uploading, so I created an object for the saveUrl property as described here.This works fine.

Only thing missing is how to handle exceptions, so when a file is rejected by the external service. If the endpoint raises an error, I now resolve to ensure the Upload control stops the Uploading message. But I want the control to stop this message and show the file in red with the error message. See my (simplified) code below. Thanks in advance for your feedback!

 

01.const onSaveRequest = (
02.       files: UploadFileInfo[],
03.       options: {
04.           formData: FormData;
05.           requestOptions: any;
06.       },
07.       onProgress: any
08.   ): Promise<{ uid: string }> => {
09.       const currentFile = files[0] as UploadFileInfo;
10.       const uid = currentFile.uid;
11.       const file = currentFile.getRawFile();
12. 
13.       const saveRequestPromise = new Promise<{ uid: string }>(async (resolve) => {
14.           ([my external service call]).then((data) => {
15.                   // all well, this works
16.                   resolve({ uid: uid });
17.               }).catch(error=>{
18.                   // how to reject so Upload control shows error for this file?
19.                   resolve({ uid: uid });
20.               });
21.           });
22.       });
23.       return saveRequestPromise;
24.   };

2 Answers, 1 is accepted

Sort by
0
Accepted
Stefan
Telerik team
answered on 11 Nov 2019, 10:02 AM

Hello, Ruud,

Thank you for the details.

This can be achieved by rejecting the promise:

reject({ uid: uid })
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject

I have updated the example:

https://stackblitz.com/edit/react-bvfjbb?file=app/main.jsx (upload a new file to see the result)

I hope this is helpful.

Regards,
Stefan
Progress Telerik

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Ruud
Top achievements
Rank 1
answered on 11 Nov 2019, 12:13 PM
Thanks! This works as expected.
Tags
General Discussions
Asked by
Ruud
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Ruud
Top achievements
Rank 1
Share this question
or