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

How to return error if JSON is being returned with data

5 Answers 655 Views
Upload
This is a migrated thread and some comments may be shown as answers.
Jaesoon
Top achievements
Rank 1
Jaesoon asked on 19 Jul 2016, 03:07 AM

I've got a situation where I want to return data from the controller if there are errors on the file i'm uploading.

 

For simple messages, i can just do [return Content("error")] which will cause the uploader to show an error message (with relevant error items - red colour, X icon etc).

 

However, i have a case where i am returning a JSON object with details of all the issues with the uploaded file like so [return Json(returnItem)].

 

I would expect the upload control to fail (as the documentation states it only accepts an empty string to be the success case) but i am getting the success behaviour from the upload control (green text).

 

How can i make it so it shows the error state when i am returning a JSON object?

 

 

Thanks

 

 

5 Answers, 1 is accepted

Sort by
0
Accepted
Veselin Tsvetanov
Telerik team
answered on 20 Jul 2016, 12:54 PM
Hi Jaesoon,

Thank you for contacting us.

By design, when the returned response is a JSON, it is interpreted by the Upload widget as a success response. The returned JSON is available deserialized in the success event handler and could be accessed by:
function onSuccess(e) {
    var returnedObject = e.response;
}

Then, based on the content of the object sent, you could manually change the appearance of the file DOM element: 
function onSuccess(e) {
    var returnedObject = e.response;
    if (returnedObject.ProductID == 56) {
        $('.k-file').removeClass('k-file-success').addClass('k-file-error');
    }
}

I hope that the above helps you. If you have any further questions, please do not hesitate to contact us.

Regards,
Veselin Tsvetanov
Telerik by Progress
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
Jaesoon
Top achievements
Rank 1
answered on 20 Jul 2016, 09:42 PM

Thanks for your help Veselin,

 

While it doesn't change control fully into the error state, trying to modify each elements manually to look like the error state will be too time consuming so we'll just stick with changing the colour to red.

 

For your reference i've added an example, i1.png shows an error (with the alert icons) whereas i2.png shows a success (with the tick icon).

0
Veselin Tsvetanov
Telerik team
answered on 21 Jul 2016, 01:25 PM
Hello Jaesoon,

You could further extend the custom styling so the failed file row looks like a true error row:
function onSuccess(e) {
    var returnedJson = e.response;
    if (returnedJson.ProductID == 56) {
        $('.k-file').removeClass('k-file-success').addClass('k-file-error');
        var statusStrong = $('.k-upload .k-file .k-upload-status');
        statusStrong.find('.k-upload-pct').removeClass('k-upload-pct').addClass('k-icon k-warning').text('');
        setTimeout(function () {
            statusStrong.find('.k-icon').removeClass('k-i-close').removeClass('k-delete').addClass('k-i-refresh k-retry').attr('title', 'Retry');
        }, 200);
    }
}

I hope that this helps you.

Regards,
Veselin Tsvetanov
Telerik by Progress
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
Tamas
Top achievements
Rank 1
answered on 07 Oct 2016, 06:14 AM

You can also set the status code to 500, then return the whatever JSON you want:

Response.StatusCode = (int)HttpStatusCode.InternalServerError;
return Content(JsonConvert.SerializeObject(new { Errors = errors }));
           

0
Dimiter Madjarov
Telerik team
answered on 07 Oct 2016, 06:26 AM

Hello guys,

As of Q3 2016 the success event of the Upload widget is preventable, which will result in displaying the file as failed. This is helpful in scenarios like the current one, when a JSON data should be returned in any case.

Regards,
Dimiter Madjarov
Telerik by Progress
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
Tags
Upload
Asked by
Jaesoon
Top achievements
Rank 1
Answers by
Veselin Tsvetanov
Telerik team
Jaesoon
Top achievements
Rank 1
Tamas
Top achievements
Rank 1
Dimiter Madjarov
Telerik team
Share this question
or