-2

I am using jquery Ajax for downloading PDF file, I generate PDF on runtime and download it, This is my Ajax call

  $.ajax({
            async: "false",
            type: "POST",
            url: "/Reports/DownloadFile?userId=" + encodeURIComponent(userId) + "&date=" + encodeURIComponent(date),
            success: function (result) {
            }
        });

and this is what I am doing in ActionResult

 public FileContentResult DownloadFile(int userId, string date, int rptType)
        {
            var userInfo = UserBLL.GetUserByID(associateId).Name;
            var dtFileDate = Convert.ToDateTime(date);
            var pdfStream = GeneratePDFStream(userId, date);//Generating Stream

if(date < DateTime.Now)
{       
return File(pdfStream, "application/pdf", string.Format("MyReport{0}{1}.pdf", userInfo.Name, dtFileDate.ToShortDateString().Replace("/", "")));
}
return null; 
        }

but its returning

Sorry, an error occurred while processing your request.

How can I download the file with Ajax call? Please note that I can't use jquery FileDownload but I can do without Ajax call, Kindly suggest I also tried with this,but same error

 var url = "/Reports/DownloadFile?userId=" + encodeURIComponent(userId) + "&date=" + encodeURIComponent(date);

        window.open(url);
Syed Salman Raza Zaidi
  • 2,172
  • 9
  • 42
  • 87

1 Answers1

0

Instead of ajax call, just try using Iframe as(demo code) :

var url = "/Reports/DownloadFile?userId=" + encodeURIComponent(userId) + "&date=" + encodeURIComponent(date) ;

var $dialog = $('<div></div>')
             .html('<iframe style="border: 0px; " src="' + url + '" width="850px" height="100%" scrolling="no"></iframe>')
              .dialog({
                  autoOpen: false,
                  modal: true,
                  height: 625,
                  width: 850,
                  title: 'YourPdfFile'
              });
        $dialog.dialog('open');
Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69