CptSupermrkt
CptSupermrkt

Reputation: 7114

Creating a hyperlink to a physical path on the local PC, to files not part of the web site

I have some files in a folder on the harddrive, like C:\ExtraContent\ that has some PDF files. This folder is not part of the website. I was able to successfully upload a PDF to this folder using the default ASP.NET FileUploader, no problem.

What I would like to do is, create a hyperlink that links to a PDF in that folder C:\ExtraContent\somePDF.pdf

I am able to get close using a Button with the following code:

protected void Button1_Click(object sender, EventArgs e)
{
    WebClient client = new WebClient();
    Byte[] buffer = client.DownloadData("C:\ExtraContent\somePDF.pdf");
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-length", buffer.Length.ToString());
    Response.BinaryWrite(buffer);
}

The above works in terms of opening the file. But I can't get this to work with an ASP.NET HyperLink.

The reason I want to use a HyperLink is so that the user can choose to right-click and Save As, to download a copy. If HyperLink controls can only link to relative paths, what can I do to get my desired result?

Note: making the files I'm trying to access part of the site is not practical for us.

Upvotes: 1

Views: 3707

Answers (2)

Maxim Gershkovich
Maxim Gershkovich

Reputation: 47169

Basically allowing access to the folder the way you describe is a real security risk (because it requires hacking at the permissions), isn't trivial and in general should be avoided. The way that you achieve your desired behaviour is something along these lines.

Firstly create a blank aspx or ashx page.

Secondly, either in the Page_Load or ProcessRequest you want to use code along the following lines

string filePath = "c:\\Documents\\Stuff\\";
string fileName = "myPath.pdf";
byte[] bytes = System.IO.File.ReadAllBytes(filePath + fileName);
context.Response.Clear();
context.Response.ContentType = "application/pdf";
context.Response.Cache.SetCacheability(HttpCacheability.Private);
context.Response.Expires = -1;
context.Response.Buffer = true;
context.Response.AddHeader("Content-Disposition", string.Format("{0};FileName=\"{1}\"", "attachment", fileName));
context.Response.BinaryWrite(bytes);
context.Response.End(); 

I haven't tested this and taken it from my head so it might need some tweeks but the above code should get you on the right track to cause the persons browser to begin downloading the file you provide.

EDIT: I just realized (after rereading your question) your problem was slightly different to what I thought, to get your issue resolved simply make the hyperlink button you are using link to a page that can process the request as described above. IE: An ashx or aspx page

Upvotes: 2

Eben Roux
Eben Roux

Reputation: 13246

You need to create a hyperlink to a page that acts as a 'proxy' so that the page will return a response that contains the file stream.

You cannot create a link to a file that is not prt of your site.

Upvotes: 0

Related Questions