简体   繁体   中英

Open external PDF file in asp.net MVC 2

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 from an other/external website, eg 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

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