简体   繁体   中英

asp.net mvc 4 download pdf not working

I want to try out to download pdf in the browser.

I found this link , and this ,

Now in my code, I followed the links and here's what I got.

Controller:

public ActionResult DownloadPdf(){
  var path = Server.MapPath("~/Doc/Pdf/sample.pdf");
  return File(path , "application/pdf", "sample.pdf");
}

View

<a href="#" id="download-pdf">Download PDF</a>

Javascript

$("#download-pdf").click(function() {
  $.post("/Home/DownloadPdf");
  return false;
});

Folder Structure:

Solution
-- Project
---- ....
---- Doc
------ Pdf
-------- sample.pdf

When I try to examine the developer's tool in chrome.

I got a 200 status and in the preview tab, I got some characters. I think it is the content of the pdf.

Now my problem is, the pdf won't show up in the browser as downloaded file.

Any help would be much appreciated. Thanks

try this

< a href="/Home/DownloadPdf" id="download-pdf">Download PDF< /a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM