PdfProcessing Capture and display error

1 Answer 35 Views
General Discussions
Richard
Top achievements
Rank 3
Iron
Iron
Iron
Richard asked on 07 Jan 2022, 05:27 PM

Good afternoon,

I've successfully managed to use PdfProcessing in a similar way to the demo but I want to include some error capture.  What's the best way to return the error to the View and display it e.g. if the file doesn't exist?

[HttpPost]
public ActionResult Download_Document()
{
    try
    {
        PdfFormatProvider formatProvider = new PdfFormatProvider();
        formatProvider.ExportSettings.ImageQuality = ImageQuality.High;

        byte[] renderedBytes = null;
        using (MemoryStream ms = new MemoryStream())
        {
            RadFixedDocument document = CreatePdfDocument.CreateDocument();
            formatProvider.Export(document, ms);
            renderedBytes = ms.ToArray();
        }

        return File(renderedBytes, "application/pdf", "PdfDocument.pdf");
     }
     catch (Exception e)
     {
         return e.Message;
     }
}

Kind regards,

Richard

1 Answer, 1 is accepted

Sort by
0
Accepted
Richard
Top achievements
Rank 3
Iron
Iron
Iron
answered on 10 Jan 2022, 02:30 PM

Good afternoon,

I have managed to come up with a solution.

The actions are called using Ajax:

$.ajax({
    type: 'GET',
    cache: false,
    dataType: 'json',
    url: '@Url.Action("Download_Document", "Controller")',
    success: function (response) {
        if (response.IsSuccessful) {
            window.location = '/Controller/Download_Pdf?fileGuid=' + response.FileGuid + '&filename=' + response.FileName;
        } else {
            alert(response.ErrorMessage);
        }
    }
});
The renderBytes array is stored in TempData, then retrieved if no exceptions have been thrown.  Otherwise, the error message is displayed.
public ActionResult Download_Document()
{
    try
    {
        PdfFormatProvider formatProvider = new PdfFormatProvider();
        formatProvider.ExportSettings.ImageQuality = ImageQuality.High;

        byte[] renderedBytes = null;
        using (MemoryStream ms = new MemoryStream())
        {
            RadFixedDocument document = CreatePdfDocument.CreateDocument();
            formatProvider.Export(document, ms);
            renderedBytes = ms.ToArray();
        }

        var guid = Guid.NewGuid().ToString();
        TempData[guid] =  renderedBytes;
        return Json( new { IsSuccessful = true, FileGuid = guid, FileName = "PdfDocument.pdf"});
     }
     catch (Exception err)
     {
        return Json( new { IsSuccessful = false, ErrorMessage = err.Message });
     }
} 

[HttpGet]
public virtual ActionResult Download_Pdf(string fileGuid, string fileName)
{
    if (TempData[fileGuid] != null)
    {
        var data = TempData[fileGuid] as byte[];
        string mimeType = "application/pdf";
        return File(data, mimeType, fileName);
    }
    else
    {
        return new EmptyResult();
    }
}

Hope this helps.

Kind regards,

Richard

Tags
General Discussions
Asked by
Richard
Top achievements
Rank 3
Iron
Iron
Iron
Answers by
Richard
Top achievements
Rank 3
Iron
Iron
Iron
Share this question
or