Providing downloads on ASP.net website
I need to provide downloads of large files (upwards of 2 GB) on an ASP.net website. It has been some time since I've done something like this (I've been in the thick-client world for awhile now), and was wondering on current best practices for this. Ideally, I would like:
- To be able to track download statistics: # of downloads is essential; actual bytes sent would be nice.
- To provide downloads in a way that "plays nice" with third-party download man开发者_运维知识库agers. Many of our users have unreliable internet connections, and being able to resume a download is a must.
- To allow multiple users to download the same file simultaneously.
My download files are not security-sensitive, so providing a direct link ("right-click to download...") is a possibility. Is just providing a direct link sufficient, letting IIS handle it, and then using some log analyzer service (any recommendations?) to compile and report the statistics? Or do I need to intercept the download request, store some info in a database, then send a custom Response? Or is there an ASP.net user control (built-in or third party) that does this? I appreciate all suggestions.
I have used custom http handlers to provide this functionality in the past. I've created a download.axd and wrote a simple HttpHandler.
public class DownloadHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
// Do some logging to track stats.
// Set some custom response headers
context.Response.TransmitFile(filename);
}
}
The important thing about the TransmitFile method is that it streams the file to the response without having to store the file in memory. This will be important in your case since you are dealing with large files.
You will also need to register the handler in your web.config:
<httpHandlers>
<add verb="GET,HEAD" path="download.axd" type="DownloadHandler, MyAssemby"/>
</httpHandlers>
Still using this approach it will be difficult to track actual bytes sent to each person downloading.
精彩评论