abenci
abenci

Reputation: 8651

Hard coded path: how do I link it on a ASP.NET MVC website?

I have a PDF document in the following location on the remote web server:

C:\Domains\Bin\Invoices\1000.pdf

How do I link it? I tried:

string rootPath = Server.MapPath("~");
string path = Path.GetFullPath(Path.Combine(rootPath, "..\\Bin\\Invoices"));

<p>@Html.Raw("<a href='"+ path + "\\" + "1000.pdf'>Original PDF copy</a>")</p>

Without success: the browser tooltip shows me file:///C:/domains/bin/invoices/1000.pdf

Thanks.

EDIT

Solved using Joe suggestion, this way:

public FileResult InvoiceInPdf(string id)
{
    string rootPath = Server.MapPath("~");
    string path = Path.GetFullPath(Path.Combine(rootPath, "..\\Bin\\Invoices\\"));

    return File(path + id + ".pdf", "application/pdf");
}

Upvotes: 0

Views: 123

Answers (1)

to StackOverflow
to StackOverflow

Reputation: 124696

You can't create a hyperlink to browse to a file at an arbitrary location on the server. Which is a good thing, as the security of your web server would otherwise be compromised.

You should create an Action that streams the file you want by returning a FileResult. Google will provide you with examples of this approach.

Upvotes: 2

Related Questions