0

I am submitting a form that is sending a request to the backend, C# action. The backend generates a pdf file and sends it back to the browser to download it. How do I catch this event? I simply want to display a message to the user when the pdf is sent from the server. I cannot use ajax because I am sending a request through the form and I expect the response to be an attachment.

        public ActionResult ConvertHTMLtoPDF(string htmltoPDfFullUrl)
        {
            Byte[] res = null;
            using (MemoryStream ms = new MemoryStream())
            {
                // not important logic

            }

            var stream = new MemoryStream(res);

            return new FileStreamResult(stream, "application/pdf")
            {
                FileDownloadName = "some name.pdf"
            };

        }
$(document).on('click', ".formSubmitDiv", function () {
    formSubmit();

})
function formSubmit() {
    $('#htmltoPDfFullUrl').val(fullHTMLLIVE);

    document.getElementById('beginConvertHTMLtoPDF').submit();
}
@using (Html.BeginForm("ConvertHTMLtoPDF", "Home", FormMethod.Post, new { id = "beginConvertHTMLtoPDF" }))
{
    <input type="hidden" name="htmltoPDfFullUrl" id="htmltoPDfFullUrl" />
}
  • Notice, that `formSubmit` is never called in your code, hence the form is probably not submitted. – Teemu Mar 18 '20 at 12:15
  • The form is being called, when you click the '.formSubmitDiv', it executes the function that calls the form. See the code above. – Tihomir Todorov Mar 18 '20 at 12:17
  • Again, your code does not call `formSubmit` function, there's just a reference to the function in the click handler, but no invocation. – Teemu Mar 18 '20 at 12:18
  • Dont you see this part of the code? document.getElementById('beginConvertHTMLtoPDF').submit(); This literally submits the form! – Tihomir Todorov Mar 18 '20 at 12:53
  • But the function containing that code is never executed ... See https://stackoverflow.com/questions/15886272/what-is-the-difference-between-a-function-call-and-function-reference Just take a look at the code in the question, that's maybe a typo in the post only ... – Teemu Mar 18 '20 at 12:58
  • Please read this https://www.w3schools.com/jsref/met_form_submit.asp – Tihomir Todorov Mar 18 '20 at 16:20
  • Please read the post I've linked above. What do you think `function () {formSubmit;}` does? It does nothing, nothing at all. – Teemu Mar 18 '20 at 16:28

1 Answers1

0

I managed to solve this by sending a cookie from the server and checking constantly for this cookie on the client.

This is how I send the cookie.

Response.Cookies.Add(new HttpCookie(serverCookieResponse + "_CookiePDFResponse", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:ff")));

This is how I check whether the cookie has been received or not

function checkCookie() {
    let cookieName = $('#serverCookieResponse').val() + "_CookiePDFResponse";
    var cookieVal = $.cookie(cookieName);
    if (cookieVal == null || cookieVal === 'undefined') {
        setTimeout("checkCookie();", 1000);
    }
    else {
        document.cookie = cookieName + '=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
    }
}

Hope this helps somebody!