views:

251

answers:

3

I have seen a couple of websites that have a dynamic download link.

They ask for a valid email address and send the dynamically created download link to that address. E.X. www.mysite.domain/hashvalue1

But how they do that, since the file exists on the domain in a specific location? www.mysite.domain/downloads

Is there any guide around this?

+1  A: 

Yes, Web URL does not have to correspond to the actual file location. The easiest way to implement this in .NET is to create an IHttpHandler which uses Response.TransmitFile based on hash value.

In this case, URL would be www.mysite.domain/file.ashx?hash=hashvalue1.

You can get better URLs if you use Routing (ASP.NET 3.5 SP1) or some kind of URL rewriter.

Andrey Shchekin
+1  A: 

They use "UrlRewrite" and a ASP.NET HttpHandler.

UrlRewrite: http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

Handler: Downlaod.ashx

using System;
using System.Web;

public class GetDownload : IHttpHandler 
{
    private static string file = "your file location";
    public void ProcessRequest (HttpContext context) 
    {
        if(UsersHasRights(context)) 
        {
            context.Response.TransmitFile(file);
        }
    }

    public bool IsReusable 
    {
        get { return false; }
    }
}
Arthur
A: 

This may not be most efficient or best way to do but can solve your problem. You have file at specific location on the server and you know it when user clicks or request for downloading. You get email address and together from file name or path and email create hash you can add some salt to it to really randomize it even if same user requests the same file again and again. Now put this hash, filename and salt (if you have one) into a database table. You can also have expirey date-time for link.

You can send user link like this site.com/files.dld?hash and implement IHttpHandler to handle this extension (see this answer by Andrey Shchekin). Here is also an article describing use of HttpHandler to provide download of zip files.

TheVillageIdiot