We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Unable to return error from api to cause 'failure' event to be triggered

Hi Syncfusion,


I am trying to return an error from an api on the upload of a document when I encounter a duplicate.  I have achieved this in the FileManager components upload functionality by returning the following:

FileManagerResponse uploadResponse = provider.CreateDocument(folderId, new List<IFormFile>() { file }, rejectDuplicate);

    if (uploadResponse.Error != null)
    {
        Response.Clear();
        Response.ContentType = "application/json; charset=utf-8";
        Response.StatusCode = int.Parse(uploadResponse.Error.Code);
        Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = uploadResponse.Error.Message;
    }

I have tried to do the same using the Uploader control in another part of our application but this doesn't go into the 'failure' event.

How do I achieve this in the Uploader control?  Also is it possible to return a custom message so I can display it to the user!?

Regards

James


6 Replies

UD UdhayaKumar Duraisamy Syncfusion Team March 2, 2023 03:40 PM UTC

Yes, you can trigger the Failure event from the server side if the uploaded files are already present in the location and send the respective error message or custom message to the failure event. Also, you can receive an error message or custom message in the failure event args, and you display the error message using the statusText.


Here is an example of how you can do this:

Client

function onFailure(e) {

  var header = e.response.headers;

  var match = header.match(/error:\s*(.*)/i);

  e.statusText = match ? match[1] : 'Unknown error';

  console.log(e.statusText);

}


Server

public void Save(IList<IFormFile> chunkFile, IList<IFormFile> UploadFiles)

        {

            long size = 0;

            // for normal upload

            try

            {

                foreach (var file in UploadFiles)

                {

                    var filename = ContentDispositionHeaderValue

.Parse(file.ContentDisposition)

                                    .FileName

                                    .Trim('"');

filename = hostingEnv.WebRootPath + $@"{filename}";

                    size += file.Length;

                    if (!System.IO.File.Exists(filename))

                    {

                        using (FileStream fs = System.IO.File.Create(filename))

                        {

file.CopyTo(fs);

fs.Flush();

                        }

                    }

                    else

                    {

                        throw new FileNotFoundException("File already exists", filename);

                    }

                }

            }

            catch (Exception e)

            {

Response.Clear();

Response.ContentType = "application/json; charset=utf-8";

Response.StatusCode = 300;

Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = "File failed to upload";

                Response.Headers.Add("Access-Control-Expose-Headers", "Error");

                Response.Headers.Add("Error", e.Message);

            }

        }


In the above code, if the file already exists, a new FileNotFoundException will be thrown with an appropriate error message and the name of the file.