开发者

ASP.Net - Open a PDF using a web method

Just wondering if it's possible to open a PDF using a web method called using AJAX? The fil开发者_开发技巧e to open is stored in a temp directory on the server.

Does anyone have any suggestions how this can be achieved?

Thanks all!


Oded has given you the best solutions but, if you still want to do it that way, here it goes:

You can use this jquery plugin.

Your web page would have an hyperlink:

<a id="PrintAjaxReport" href="javascript:{}">Print report Ajax</a>

jQuery:

$(document).ready(function() {
    $("#PrintAjaxRepor").click(function() {
         $.download('PdfReport.aspx', "filename=mySpreadsheet", "POST");
     });
});

PdfReport.aspx

public partial class PdfReport : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var pdfDocumentName = Request.Params["filename"].ToString() + ".pdf";
        var myReport = "Razor Syntax Quick Reference.pdf";

        var FileName = Path.Combine(Path.Combine(Server.MapPath("~"), "Temp"), myReport);

        Response.Clear();
        Response.ClearHeaders();
        Response.ClearContent();

        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Type", "application/pdf");
        Response.AddHeader("content-disposition", "attachment; filename=" + pdfDocumentName);
        Response.TransmitFile(FileName);
        Response.End();
    }
}

You can find a sample (OpenPDFjQuery) here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜