2

I am making an ajax call to the controller. There i am creating a pdf file.

How can i sent it to the browser so that user can download it.

    [HttpPost]
    public void Createpdf(string htmlpage)
    {
        var htmlToPdfConverter = new HtmlToPdf();
        string htmlCode = "<!DOCTYPE html><html lang='en'><head><title>Report</title>/head><body>" 
            + htmlpage 
            + "</body></html>";
        string baseUrl = null;
        pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(htmlCode, baseUrl);
        System.IO.File.WriteAllBytes(@"D:\HubReport.pdf", pdfBuffer);
        //System.Web.HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf");
        //System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition",
        //   String.Format("{0}; filename=HubReport.pdf;size={1}", "attachment", pdfBuffer.Length.ToString()));
        //System.Web.HttpContext.Current.Response.BinaryWrite(pdfBuffer);
        //System.Web.HttpContext.Current.Response.End();
    }
ghost...
  • 893
  • 1
  • 14
  • 32
  • Have a look at [this question](http://stackoverflow.com/questions/3604562/download-file-of-any-type-in-asp-net-mvc-using-fileresult). – Andrei V Mar 26 '14 at 07:35
  • Set `Response.ContentType` to `application/x-pdf` – Nilesh Mar 26 '14 at 07:41
  • As far as I know, you can't directly download a file using AJAX. Instead, you should point your browser (in your AJAX response event) to the URL of the download file. – Andrei V Mar 26 '14 at 07:45
  • yes exactly...how can i point it – ghost... Mar 26 '14 at 07:55
  • My bad. I overlooked the post, sorry about that. You are anyways writing the file to the disk. So you can return the Url from the controller and then just open that in a new window.Or This Url can be a handler or a MVC action which gets the file for you when you pass on the file name to it. – Nilesh Mar 26 '14 at 08:28
  • i am new to mvc and c#...can you please give me code or some link – ghost... Mar 26 '14 at 08:29

1 Answers1

1

I havent tested this but this might give you some pointers.

You pass your file name to this action. and give this action as the url of new window.

public FileResult Getfile(string downloadfileName)
{
    byte[] fl = System.IO.File.ReadAllBytes(@"C:\temp\" + downloadfileName);
    string fileName = "Somefile.pdf";
    return File(fl, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}

EDIT

I tested this and it works absolutely fine

here is my Sample View Code

@{
    ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
<script type="text/javascript">
    $(document).ready(function () {
        //You need to call this click function from you ajax Success callback.
        $("#getFile").click(function () {
            window.open('@Url.Action("GetFile","Home","test.pdf")', "downloadfile");
        });
    });

</script>
<input type="button" value="GetFile" id="getFile" />

Here is the Controller

public class HomeController : BaseController
{
    public ActionResult Index()
    {
        return View();
    }

    public FileResult Getfile(string downloadFileName)
    {
        byte[] fl = System.IO.File.ReadAllBytes(@"C:\temp\sampleFile.pdf");
        string fileName = "Somefile.pdf";
        return File(fl, System.Net.Mime.MediaTypeNames.Application.Octet);
    }
}

And here is the output

Sample output

Nilesh
  • 2,247
  • 4
  • 18
  • 32
  • It didn't work because `window.open` initiating an HTTP **GET** request, while the OP Action (`Createpdf`) is marked with `[HttpPost]`. – haim770 Mar 26 '14 at 10:45
  • I know the action is marked as **POST**. What I meant to say was after creating a file another `Action` would be called which downloads the file. The original `CreatePDF` action would send the created file name which will be passed on to the `Getfile` Action – Nilesh Mar 26 '14 at 11:07