Kendo Upload Control, Pass custom error message back to view Kendo Upload Control, Pass custom error message back to view jquery jquery

Kendo Upload Control, Pass custom error message back to view


The solution is simple, once you see it, but the documentation on this is not complete. We know that we should return an empty string to signify success. I have also seen – but cannot find right now – docs that suggest you could alternately return a JSON object instead of string.Empty, and that too would signify success. Anything other than JSON object or string.Empty signifies an error. But how do we handle it?

The argument sent to the Error event handler still has the basic components: e.files, e.operation, and e.XMLHttpRequest. The solution is to return a normal string in your controller’s method, and it becomes the raw XMLHttpRequest.responseText. It is not JSON, so don’t try to parse as JSON.

Controller:

    public ActionResult Index(IEnumerable<HttpPostedFileBase> files)    {        try        {            // … do something            return Content(string.Empty);        }        catch (Exception ex)        {            // simply send the error message to Content            return Content(ex.Message);        }    }

JavaScript handler:

    function onError(e) {        // invalid because the responseText is a raw string, not JSON        //var err = $.parseJSON(e.XMLHttpRequest.responseText);        var err = e.XMLHttpRequest.responseText;        alert(err);    };


To handle as an invalid upload, had to change the http return code.

In catch clause:

HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;


Sfuqua's answer (thanks!) clarified and working for me:

function onError(e) {    var err = e.XMLHttpRequest.responseText;    alert(err);     e.stopPropagation();    return false;};

In this case, onUploadSuccess() was not triggered.