Есть несколько способов сделать это.Вот как вы можете это сделать.
Вместо того, чтобы отправлять файл с диска по прямой ссылке, такой как <a href="http://mysite.com/music/file.exe"></a>
, напишите HttpHandler
для загрузки файла.В HttpHandler вы можете обновить количество скачиваемых файлов в базе данных.
Загрузка файла 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; }
}
}
Конфигурация Web.config
//httphandle configuration in your web.config
<httpHandlers>
<add verb="GET" path="FileDownload.ashx" type="DownloadHandler"/>
</httpHandlers>
Связывание загрузки файла с внешнего интерфейса
//in your front-end website pages, html,aspx,php whatever.
<a href="FileDownload.ashx?filename=file.exe">Download file.exe</a>
Дополнительно , вы можете отобразить расширение exe
в Интернете.config для HttpHandler.Для этого вам нужно убедиться, что вы настроили свой IIS для пересылки запросов расширения .exe рабочему процессу asp.net, а не для непосредственного обслуживания, а также убедитесь, что mp3-файл не находится в том же месте, что и обработчик,если файл найден на диске в том же месте, то HttpHandler будет перезаписан, и файл будет отправлен с диска.
<httpHandlers>
<add verb="GET" path="*.exe" type="DownloadHandler"/>
</httpHandlers>