Unable to download generated PDF when controller action is called from Ajax [duplicate]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0
















This question already has an answer here:




  • Download a file by jQuery.Ajax

    17 answers




I am returning FilestreamResult from controller
I tried both way jquery and ajax but cant succeed for download pdf/xlsx file from controller.



Controller:



    var filestream = new FileStream(pdfoutputpath + ".pdf", FileMode.Open);
return new FileStreamResult(filestream, "application/pdf");


View code using jquery:



function downloadpdffile(id) {
$(".popup-overlay, .popup-content").removeClass("active");
var url = "/WorkInstructions/WorkinstructionDownload";
$.get(url, { id: id, fileformat: 2 }, function () {

});
}


using ajax:



 function downloadpdffile(id) {
$(".popup-overlay, .popup-content").removeClass("active");
$.ajax({
url: "/WorkInstructions/WorkinstructionDownload",
cache: false,
type: "GET",
data: { id: id, fileformat: 2 },
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function () {

}

});









share|improve this question













marked as duplicate by user3559349 Nov 16 '18 at 20:49


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • What happens if you change your ajax to POST? Also, how is your controller method decorated? HttpGet, HttpPost, etc. Does your controller method get hit when the call is made? Add the error callback to your ajax request and place something in the body of the success and error callback functions and see which one is hit, if the error is hit, you can get some information from the XHR object.

    – Ryan Wilson
    Nov 16 '18 at 13:26













  • can you show your complete controller code?

    – Negi Rox
    Nov 16 '18 at 13:29











  • is it necessary to use ajax or you just want to download that pdf. or xls file?

    – Negi Rox
    Nov 16 '18 at 13:30











  • If you need to receive a file via ajax I would refer to this SO post (stackoverflow.com/questions/4545311/…)

    – Ryan Wilson
    Nov 16 '18 at 14:02


















0
















This question already has an answer here:




  • Download a file by jQuery.Ajax

    17 answers




I am returning FilestreamResult from controller
I tried both way jquery and ajax but cant succeed for download pdf/xlsx file from controller.



Controller:



    var filestream = new FileStream(pdfoutputpath + ".pdf", FileMode.Open);
return new FileStreamResult(filestream, "application/pdf");


View code using jquery:



function downloadpdffile(id) {
$(".popup-overlay, .popup-content").removeClass("active");
var url = "/WorkInstructions/WorkinstructionDownload";
$.get(url, { id: id, fileformat: 2 }, function () {

});
}


using ajax:



 function downloadpdffile(id) {
$(".popup-overlay, .popup-content").removeClass("active");
$.ajax({
url: "/WorkInstructions/WorkinstructionDownload",
cache: false,
type: "GET",
data: { id: id, fileformat: 2 },
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function () {

}

});









share|improve this question













marked as duplicate by user3559349 Nov 16 '18 at 20:49


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • What happens if you change your ajax to POST? Also, how is your controller method decorated? HttpGet, HttpPost, etc. Does your controller method get hit when the call is made? Add the error callback to your ajax request and place something in the body of the success and error callback functions and see which one is hit, if the error is hit, you can get some information from the XHR object.

    – Ryan Wilson
    Nov 16 '18 at 13:26













  • can you show your complete controller code?

    – Negi Rox
    Nov 16 '18 at 13:29











  • is it necessary to use ajax or you just want to download that pdf. or xls file?

    – Negi Rox
    Nov 16 '18 at 13:30











  • If you need to receive a file via ajax I would refer to this SO post (stackoverflow.com/questions/4545311/…)

    – Ryan Wilson
    Nov 16 '18 at 14:02














0












0








0









This question already has an answer here:




  • Download a file by jQuery.Ajax

    17 answers




I am returning FilestreamResult from controller
I tried both way jquery and ajax but cant succeed for download pdf/xlsx file from controller.



Controller:



    var filestream = new FileStream(pdfoutputpath + ".pdf", FileMode.Open);
return new FileStreamResult(filestream, "application/pdf");


View code using jquery:



function downloadpdffile(id) {
$(".popup-overlay, .popup-content").removeClass("active");
var url = "/WorkInstructions/WorkinstructionDownload";
$.get(url, { id: id, fileformat: 2 }, function () {

});
}


using ajax:



 function downloadpdffile(id) {
$(".popup-overlay, .popup-content").removeClass("active");
$.ajax({
url: "/WorkInstructions/WorkinstructionDownload",
cache: false,
type: "GET",
data: { id: id, fileformat: 2 },
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function () {

}

});









share|improve this question















This question already has an answer here:




  • Download a file by jQuery.Ajax

    17 answers




I am returning FilestreamResult from controller
I tried both way jquery and ajax but cant succeed for download pdf/xlsx file from controller.



Controller:



    var filestream = new FileStream(pdfoutputpath + ".pdf", FileMode.Open);
return new FileStreamResult(filestream, "application/pdf");


View code using jquery:



function downloadpdffile(id) {
$(".popup-overlay, .popup-content").removeClass("active");
var url = "/WorkInstructions/WorkinstructionDownload";
$.get(url, { id: id, fileformat: 2 }, function () {

});
}


using ajax:



 function downloadpdffile(id) {
$(".popup-overlay, .popup-content").removeClass("active");
$.ajax({
url: "/WorkInstructions/WorkinstructionDownload",
cache: false,
type: "GET",
data: { id: id, fileformat: 2 },
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function () {

}

});




This question already has an answer here:




  • Download a file by jQuery.Ajax

    17 answers








jquery asp.net ajax asp.net-mvc-4 asp.net-core






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 13:24









Arun SolankiArun Solanki

1189




1189




marked as duplicate by user3559349 Nov 16 '18 at 20:49


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by user3559349 Nov 16 '18 at 20:49


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • What happens if you change your ajax to POST? Also, how is your controller method decorated? HttpGet, HttpPost, etc. Does your controller method get hit when the call is made? Add the error callback to your ajax request and place something in the body of the success and error callback functions and see which one is hit, if the error is hit, you can get some information from the XHR object.

    – Ryan Wilson
    Nov 16 '18 at 13:26













  • can you show your complete controller code?

    – Negi Rox
    Nov 16 '18 at 13:29











  • is it necessary to use ajax or you just want to download that pdf. or xls file?

    – Negi Rox
    Nov 16 '18 at 13:30











  • If you need to receive a file via ajax I would refer to this SO post (stackoverflow.com/questions/4545311/…)

    – Ryan Wilson
    Nov 16 '18 at 14:02



















  • What happens if you change your ajax to POST? Also, how is your controller method decorated? HttpGet, HttpPost, etc. Does your controller method get hit when the call is made? Add the error callback to your ajax request and place something in the body of the success and error callback functions and see which one is hit, if the error is hit, you can get some information from the XHR object.

    – Ryan Wilson
    Nov 16 '18 at 13:26













  • can you show your complete controller code?

    – Negi Rox
    Nov 16 '18 at 13:29











  • is it necessary to use ajax or you just want to download that pdf. or xls file?

    – Negi Rox
    Nov 16 '18 at 13:30











  • If you need to receive a file via ajax I would refer to this SO post (stackoverflow.com/questions/4545311/…)

    – Ryan Wilson
    Nov 16 '18 at 14:02

















What happens if you change your ajax to POST? Also, how is your controller method decorated? HttpGet, HttpPost, etc. Does your controller method get hit when the call is made? Add the error callback to your ajax request and place something in the body of the success and error callback functions and see which one is hit, if the error is hit, you can get some information from the XHR object.

– Ryan Wilson
Nov 16 '18 at 13:26







What happens if you change your ajax to POST? Also, how is your controller method decorated? HttpGet, HttpPost, etc. Does your controller method get hit when the call is made? Add the error callback to your ajax request and place something in the body of the success and error callback functions and see which one is hit, if the error is hit, you can get some information from the XHR object.

– Ryan Wilson
Nov 16 '18 at 13:26















can you show your complete controller code?

– Negi Rox
Nov 16 '18 at 13:29





can you show your complete controller code?

– Negi Rox
Nov 16 '18 at 13:29













is it necessary to use ajax or you just want to download that pdf. or xls file?

– Negi Rox
Nov 16 '18 at 13:30





is it necessary to use ajax or you just want to download that pdf. or xls file?

– Negi Rox
Nov 16 '18 at 13:30













If you need to receive a file via ajax I would refer to this SO post (stackoverflow.com/questions/4545311/…)

– Ryan Wilson
Nov 16 '18 at 14:02





If you need to receive a file via ajax I would refer to this SO post (stackoverflow.com/questions/4545311/…)

– Ryan Wilson
Nov 16 '18 at 14:02












2 Answers
2






active

oldest

votes


















0














you can use window.open function to achieve your task if there is no problem in your controller. I used same technique in my project.



var downloadURL='@Url.Action("WorkinstructionDownload", "WorkInstructions", new { id = id, fileformat = 2})'
window.open(downloadURL)





share|improve this answer































    0














    Try putting this in your success function of your ajax call:



    success: function(){
    window.location = '@Url.Action("WorkinstructionDownload", "WorkInstructions", new { id = id, fileformat = 2})';
    }


    although I don't think ajax is providing any value here unless I'm missing something. You could easily take the contents of that success function I posted above and put it outside of the ajax call altogether (and delete the ajax call). Or you could just simply do the following and remove the script altogether:



    <a href="@Url.Action("WorkinstructionDownload", "WorkInstructions", new { id = id, fileformat = 2 })">Download Form</a>






    share|improve this answer


























    • well, apriciate your work but I tried it's not working.

      – Arun Solanki
      Nov 16 '18 at 14:50











    • I've put together use cases for both options I listed which do not use ajax and they are working. Can you provide more information as to what's going wrong? Are you receiving an error? I suspect you have an issue with your controller action if this isn't working. In that case, you'll need to post your entire controller action to your question

      – GregH
      Nov 16 '18 at 14:53











    • Now I found another solution and it's working,I customly creating ancor tag <a> and calling from js and working as needed.

      – Arun Solanki
      Nov 17 '18 at 15:44


















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    you can use window.open function to achieve your task if there is no problem in your controller. I used same technique in my project.



    var downloadURL='@Url.Action("WorkinstructionDownload", "WorkInstructions", new { id = id, fileformat = 2})'
    window.open(downloadURL)





    share|improve this answer




























      0














      you can use window.open function to achieve your task if there is no problem in your controller. I used same technique in my project.



      var downloadURL='@Url.Action("WorkinstructionDownload", "WorkInstructions", new { id = id, fileformat = 2})'
      window.open(downloadURL)





      share|improve this answer


























        0












        0








        0







        you can use window.open function to achieve your task if there is no problem in your controller. I used same technique in my project.



        var downloadURL='@Url.Action("WorkinstructionDownload", "WorkInstructions", new { id = id, fileformat = 2})'
        window.open(downloadURL)





        share|improve this answer













        you can use window.open function to achieve your task if there is no problem in your controller. I used same technique in my project.



        var downloadURL='@Url.Action("WorkinstructionDownload", "WorkInstructions", new { id = id, fileformat = 2})'
        window.open(downloadURL)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 13:34









        Negi RoxNegi Rox

        1,9141512




        1,9141512

























            0














            Try putting this in your success function of your ajax call:



            success: function(){
            window.location = '@Url.Action("WorkinstructionDownload", "WorkInstructions", new { id = id, fileformat = 2})';
            }


            although I don't think ajax is providing any value here unless I'm missing something. You could easily take the contents of that success function I posted above and put it outside of the ajax call altogether (and delete the ajax call). Or you could just simply do the following and remove the script altogether:



            <a href="@Url.Action("WorkinstructionDownload", "WorkInstructions", new { id = id, fileformat = 2 })">Download Form</a>






            share|improve this answer


























            • well, apriciate your work but I tried it's not working.

              – Arun Solanki
              Nov 16 '18 at 14:50











            • I've put together use cases for both options I listed which do not use ajax and they are working. Can you provide more information as to what's going wrong? Are you receiving an error? I suspect you have an issue with your controller action if this isn't working. In that case, you'll need to post your entire controller action to your question

              – GregH
              Nov 16 '18 at 14:53











            • Now I found another solution and it's working,I customly creating ancor tag <a> and calling from js and working as needed.

              – Arun Solanki
              Nov 17 '18 at 15:44
















            0














            Try putting this in your success function of your ajax call:



            success: function(){
            window.location = '@Url.Action("WorkinstructionDownload", "WorkInstructions", new { id = id, fileformat = 2})';
            }


            although I don't think ajax is providing any value here unless I'm missing something. You could easily take the contents of that success function I posted above and put it outside of the ajax call altogether (and delete the ajax call). Or you could just simply do the following and remove the script altogether:



            <a href="@Url.Action("WorkinstructionDownload", "WorkInstructions", new { id = id, fileformat = 2 })">Download Form</a>






            share|improve this answer


























            • well, apriciate your work but I tried it's not working.

              – Arun Solanki
              Nov 16 '18 at 14:50











            • I've put together use cases for both options I listed which do not use ajax and they are working. Can you provide more information as to what's going wrong? Are you receiving an error? I suspect you have an issue with your controller action if this isn't working. In that case, you'll need to post your entire controller action to your question

              – GregH
              Nov 16 '18 at 14:53











            • Now I found another solution and it's working,I customly creating ancor tag <a> and calling from js and working as needed.

              – Arun Solanki
              Nov 17 '18 at 15:44














            0












            0








            0







            Try putting this in your success function of your ajax call:



            success: function(){
            window.location = '@Url.Action("WorkinstructionDownload", "WorkInstructions", new { id = id, fileformat = 2})';
            }


            although I don't think ajax is providing any value here unless I'm missing something. You could easily take the contents of that success function I posted above and put it outside of the ajax call altogether (and delete the ajax call). Or you could just simply do the following and remove the script altogether:



            <a href="@Url.Action("WorkinstructionDownload", "WorkInstructions", new { id = id, fileformat = 2 })">Download Form</a>






            share|improve this answer















            Try putting this in your success function of your ajax call:



            success: function(){
            window.location = '@Url.Action("WorkinstructionDownload", "WorkInstructions", new { id = id, fileformat = 2})';
            }


            although I don't think ajax is providing any value here unless I'm missing something. You could easily take the contents of that success function I posted above and put it outside of the ajax call altogether (and delete the ajax call). Or you could just simply do the following and remove the script altogether:



            <a href="@Url.Action("WorkinstructionDownload", "WorkInstructions", new { id = id, fileformat = 2 })">Download Form</a>







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 16 '18 at 13:36

























            answered Nov 16 '18 at 13:31









            GregHGregH

            2,98311952




            2,98311952













            • well, apriciate your work but I tried it's not working.

              – Arun Solanki
              Nov 16 '18 at 14:50











            • I've put together use cases for both options I listed which do not use ajax and they are working. Can you provide more information as to what's going wrong? Are you receiving an error? I suspect you have an issue with your controller action if this isn't working. In that case, you'll need to post your entire controller action to your question

              – GregH
              Nov 16 '18 at 14:53











            • Now I found another solution and it's working,I customly creating ancor tag <a> and calling from js and working as needed.

              – Arun Solanki
              Nov 17 '18 at 15:44



















            • well, apriciate your work but I tried it's not working.

              – Arun Solanki
              Nov 16 '18 at 14:50











            • I've put together use cases for both options I listed which do not use ajax and they are working. Can you provide more information as to what's going wrong? Are you receiving an error? I suspect you have an issue with your controller action if this isn't working. In that case, you'll need to post your entire controller action to your question

              – GregH
              Nov 16 '18 at 14:53











            • Now I found another solution and it's working,I customly creating ancor tag <a> and calling from js and working as needed.

              – Arun Solanki
              Nov 17 '18 at 15:44

















            well, apriciate your work but I tried it's not working.

            – Arun Solanki
            Nov 16 '18 at 14:50





            well, apriciate your work but I tried it's not working.

            – Arun Solanki
            Nov 16 '18 at 14:50













            I've put together use cases for both options I listed which do not use ajax and they are working. Can you provide more information as to what's going wrong? Are you receiving an error? I suspect you have an issue with your controller action if this isn't working. In that case, you'll need to post your entire controller action to your question

            – GregH
            Nov 16 '18 at 14:53





            I've put together use cases for both options I listed which do not use ajax and they are working. Can you provide more information as to what's going wrong? Are you receiving an error? I suspect you have an issue with your controller action if this isn't working. In that case, you'll need to post your entire controller action to your question

            – GregH
            Nov 16 '18 at 14:53













            Now I found another solution and it's working,I customly creating ancor tag <a> and calling from js and working as needed.

            – Arun Solanki
            Nov 17 '18 at 15:44





            Now I found another solution and it's working,I customly creating ancor tag <a> and calling from js and working as needed.

            – Arun Solanki
            Nov 17 '18 at 15:44



            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues