0

I want to return a link to a PDF which will initiate a download on the client-side. I have a method that looks like this:

public ActionResult DownloadPdf()
{
    return File("bitcoin.org/bitcoin.pdf", "application/pdf", Server.UrlEncode("bitcoin.org/bitcoin.pdf");
}

I always get a System.IO.DirectoryNotFoundException because my URL gets used as a relative path (something like C:\mydir\bitcoin.org\bitcoin.pdf is searched for and not found). How can I return something hosted elsewhere?

1 Answer 1

0

To return a file hosted off-site as a FileResult, you need to download the file first, and then return that to the client. Something like this should work:

public ActionResult DownloadPdf()
{
    using (var client = new WebClient())
    {
        byte[] bytes = client.DownloadData("http://bitcoin.org/bitcoin.pdf");
        return File(bytes, "application/pdf", Server.UrlEncode("bitcoin.org/bitcoin.pdf"));
    }
}

I don't know what your requirements are, but it seems like it would be easier to just link to the off-site resource, or host it yourself so you don't have to download it to the server every time it is requested.

To my knowledge, this is the only way to can return a file you don't host as a FileResult.

If you have any questions, or if anything is unclear, please let me know.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.