1

I have a graph in svg format which on client side I am converting it to get Base64 string to send it to server. Server will then use that svg data along with other parameter to generate PDF using PdfRpt.Core and will send back to client.

How to send that base64 string to MVC controller. I am getting request URI too long error and all parameter on server side are received as null

Any help is highly appreciated!

Javascript Code

function DownloadPdfServer() {
    html2canvas($("#MyDiv")[0]).then(function (canvas) {
        debugger;
        var base64Data = canvas.toDataURL("image/jpeg");
        var url = "/MyController/Action";
        var startDt = parseInt($('#StartDate').val(), 10);
        var endDt = parseInt($('#EndDate').val(), 10);

        var req = new XMLHttpRequest();
        var params = 'StartDate=' + startDt + '&EndDate=' + endDt + '&base64Data=' + base64Data;
        req.open("POST", url, true);
        req.responseType = "blob";
        req.onreadystatechange = function () {
            if (req.readyState === 4 && req.status === 200) {
                if (typeof window.navigator.msSaveBlob === 'function') {
                    window.navigator.msSaveBlob(req.response, "PdfName-" + new Date().getTime() + ".pdf");
                } else {
                    var blob = req.response;
                    var link = document.createElement('a');
                    link.href = window.URL.createObjectURL(blob);
                    link.download = "PdfName-" + new Date().getTime() + ".pdf";
                    // append the link to the document body
                    document.body.appendChild(link);
                    link.click();
                }
            }
        };
        req.send(params);
    });
    return false;
}
document.getElementById('PrintPDF').addEventListener('click', DownloadPdfServer);


Server Side Code:

public class MyController : BaseController
    {
        public async Task<ActionResult> Action(string StartDate, string EndDate, string base64Data)
        {
            string APIUrl = "Data/Get?StartDt=" + StartDate + "&EndDt=" + EndDate;
            try
            {
                // get data from api
                var userCriteria = await GetListFromApiAsync<CustomDto>(APIUrl);

                var reportBytes = PdfReport.CreateInMemoryPdfReport(userCriteria, StartDate, EndDate, base64Data);
                return File(reportBytes, "application/pdf");
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
Alvin
  • 240
  • 4
  • 23

0 Answers0