0

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 () {

                    }

                });
Arun Solanki
  • 174
  • 11
  • 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 (https://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax) – Ryan Wilson Nov 16 '18 at 14:02

2 Answers2

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>

GregH
  • 5,125
  • 8
  • 55
  • 109
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)
Negi Rox
  • 3,828
  • 1
  • 11
  • 18