0

I have a page which allows the user to download e-books which is in pdf format.I have written the following code for performing the download.

Controller

 [HttpPost]
    public ActionResult Ebook(string url,string fileName)
    {
        try
        {               
            //url="~/ebook/java.pdf"
            string filePath = Server.MapPath(url);
            Response.ContentType = "application/pdf";
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
            Response.WriteFile(filePath);
            Response.End();

            return Json("success",JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            return Json("error", JsonRequestBehavior.AllowGet);
        }
    }

Javascript

 $(document).on("click", ".btnDownload", function () {

    var downloadurl = $(this).data("download-url");
    var fileName = $(this).data("file-name");
    var href = $(this).attr("href");       

    $.ajax({
        type: "POST",
        url: href,
        data: { url: downloadurl, fileName: fileName },
        datatype: "json",
        success: function (data) {
            if (data = "success") {
                bootbox.alert("Successfully downloaded the e-book");
            }
            else {
                bootbox.alert("Cannot download this e-book");
            }
        },
        error: function (err) {
            bootbox.alert("Cannot download this e-book");
        }
    });

    return false;
});

CSHTML

 <table id="tblPartnerDetails" class="table table-bordered table-hover">
   <thead>
      <tr>
         <th style="width:10px"></th>
         <th>Course Name</th>
         <th style="width:10px"></th>
      </tr>
    </thead>
    <tbody>
        @for (int i = 0; i < Model.StudentFeedback.Count; i++)
        {
            <tr>
                <td>
                    @(i + 1)
                </td>
                <td>
                    @Model.StudentFeedback[i].Course.Name
                </td>
                <td>
                    <a class="btn btn-primary pull-left btn-xs btnDownload" href="@Url.Action("Ebook")" data-download-url="@Model.StudentFeedback[i].Course.CourseDownloadUrl" 
                                                       style="margin-right: 5px;" data-file-name="@Model.StudentFeedback[i].Course.Name">
                                                        <i class="fa fa-download"></i> Download
                    </a>
               </td>
            </tr>
         }
  </tbody>
 </table>

The problem is that file is not getting downloaded and no error message is showing.

ksg
  • 3,927
  • 7
  • 51
  • 97
  • Possible duplicate of [Download Excel file via AJAX MVC](http://stackoverflow.com/questions/16670209/download-excel-file-via-ajax-mvc) –  Feb 03 '16 at 05:23
  • I believe your browser will block the file downloaded via ajax, you could achieve this using a form post. See the answer posted here. http://stackoverflow.com/questions/35079544/net-mvc-4-application-call-from-ajax-to-a-function-in-controller/35089099#35089099 – Emma Middlebrook Feb 03 '16 at 11:31

0 Answers0