thi*_*eek 12
是的,有几种方法可以做到这一点.这是你如何做到这一点.
而不是像直接链接那样从磁盘上提供mp3文件,而是<a href="http://mysite.com/music/song.mp3"></a>写一个HttpHandler服务文件下载.在HttpHandler中,您可以更新数据库中的file-download-count.
文件下载HttpHandler
//your http-handler
public class DownloadHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string fileName = context.Request.QueryString["filename"].ToString();
string filePath = "path of the file on disk"; //you know where your files are
FileInfo file = new System.IO.FileInfo(filePath);
if (file.Exists)
{
try
{
//increment this file download count into database here.
}
catch (Exception)
{
//handle the situation gracefully.
}
//return the file
context.Response.Clear();
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
context.Response.AddHeader("Content-Length", file.Length.ToString());
context.Response.ContentType = "application/octet-stream";
context.Response.WriteFile(file.FullName);
context.ApplicationInstance.CompleteRequest();
context.Response.End();
}
}
public bool IsReusable
{
get { return true; }
}
}
Run Code Online (Sandbox Code Playgroud)
Web.config配置
//httphandle configuration in your web.config
<httpHandlers>
<add verb="GET" path="FileDownload.ashx" type="DownloadHandler"/>
</httpHandlers>
Run Code Online (Sandbox Code Playgroud)
从前端链接文件下载
//in your front-end website pages, html,aspx,php whatever.
<a href="FileDownload.ashx?filename=song.mp3">Download Song3.mp3</a>
Run Code Online (Sandbox Code Playgroud)
此外,您可以将mp3web.config中的扩展名映射到HttpHandler.要做到这一点,你必须确保,你配置你的IIS将.mp3扩展请求转发到asp.net工作进程而不是直接提供,并确保mp3文件不在处理程序捕获的同一位置,如果在同一位置的磁盘上找到该文件,则HttpHandler将被覆盖,并且该文件将从磁盘提供.
<httpHandlers>
<add verb="GET" path="*.mp3" type="DownloadHandler"/>
</httpHandlers>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6504 次 |
| 最近记录: |