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:
<a href="@Href("./Page1/abcd.doc")">Download</a>
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?
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.
Bronzato
Member
2 Points
6 Posts
Download link for a .doc file
Dec 29, 2010 07:24 AM|LINK
Hi,
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:
<a href="@Href("./Page1/abcd.doc")">Download</a>
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.
Lyra Belaqua
Contributor
2673 Points
552 Posts
Re: Download link for a .doc file
Dec 29, 2010 08:46 AM|LINK
Hi Bronzato,
Please find the code below, which is for pdf file
Response.Clear(); Response.AppendHeader("content-disposition", "attachment; filename=xxxxxx.pdf"); Response.ContentType = "text/plain"; Response.WriteFile(Server.MapPath("/xxxxxxx.pdf")); Response.Flush(); Response.End();You can same use for doc,txt or any other extension.
For more detail http://www.google.com/search?hl=en&q=open+save+dialogue+box+%2B+c%23&aq=f&aqi=&aql=&oq=&gs_rfai=
Please don't forget to click "Mark as Answer" on the post that helped you.
Before printing, think about the environment, every 3000 A4 paper costs 1 tree.
Mikesdotnett...
All-Star
154951 Points
19870 Posts
Moderator
MVP
Re: Download link for a .doc file
Dec 29, 2010 11:16 AM|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: http://www.mikesdotnetting.com/Article/122/Simple-File-Download-Protection-with-ASP.NET
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter