开发者

Open external PDF file in asp.net MVC 2

开发者 https://www.devze.com 2023-02-08 07:14 出处:网络
I know how to open an internal pdf file : public ActionResult GetPDF( string filename ) { return File( filename, \"application/pdf\", Server.HtmlEncode( filename ) );

I know how to open an internal pdf file :

public ActionResult GetPDF( string filename )
{
    return File( filename, "application/pdf", Server.HtmlEncode( filename ) );
}

question is, how to open a PDF file f开发者_StackOverflow中文版rom an other/external website, e.g. http://example.com/mypdffile.pdf


You don't really need a controller action to do this. You could simply:

<a href="http://www.blabla.com/mypdffile.pdf">Open mypdffile.pdf</a>

Of course if you want to hide this address from the user you could use a WebClient to fetch it on the server:

public ActionResult GetPDF() 
{ 
    using (var client = new WebClient())
    {
        var buffer = client.DownloadData("http://www.blabla.com/mypdffile.pdf");
        return File(buffer, "application/pdf", "mypdffile.pdf");
    }
}

And in your view:

<%= Html.ActionLink("Download PDF", "GetPDF") %>


You will need it locally anyway to do any processing so, you can download it to local folder and then show it. use WebClient or HttpRequest/HttpResponse objects to do the downloading

0

精彩评论

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