Last post Dec 29, 2010 12:16 PM by Mikesdotnetting
Dec 29, 2010 08:24 AM|Bronzato|LINK
I would like to create a link for visitors to download a Word document. But I would like to be sure my visitor is authenticated before allowing him to download something. I try something like this:
This link is routed to the same page and some server script (razor) check if the user is connected.
The problem is when this link is clicked, an error is returned because I have a file with an extension in the link...And thus the system try to reach the file (which doesn't exist at this place). I have no error if I remove the extension (from
abcd.doc) but then I have to add extension later for the download.
So, how can I proceed?
Sorry if it is not clear.
Dec 29, 2010 09:46 AM|Lyra Belaqua|LINK
Please find the code below, which is for pdf file
Response.AppendHeader("content-disposition", "attachment; filename=xxxxxx.pdf");
Response.ContentType = "text/plain";
You can same use for doc,txt or any other extension.
For more detail
Dec 29, 2010 12:16 PM|Mikesdotnetting|LINK
If you store the file in a folder under the Root directory of the site, there is always the possibility that someone will guess the URL and browse to it. You need to store your files in a non-browsable location such as App_Data, or outside the root of your
site. Then you can use a cshtml file to authenticate the user before streaming the file using the code above.
The general idea (although this example uses an HttpHandler instead of a cshtml file) can be got here: