如何从URL下载文件并管理C#中的下载计数

时间:2018-07-04 08:18:48

标签: c# asp.net

我有download.aspx页。在Page_Load事件中,我从url下载文件并增加下载次数并存储到数据库。

刷新页面时,下载数量增加。如果我拒绝下载,数量也会增加。

我想从浏览器保存文件时增加计数。

我的代码如下

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string url = "http://localhost:5045/Documents/sample.pdf";                        
            DownLoadFile(url);
            //This is code for download counter do entry in database
            SoftwareController.SoftwareDownloadHistoryEdit(0, Id, CommonController.GETMyIP(), false, "ADD");               
        }
    }

    protected void DownLoadFile(string URL)
    {
        System.Net.WebClient net = new System.Net.WebClient();
        Response.ClearHeaders();
        Response.Clear();
        Response.Expires = 0;
        Response.Buffer = true;
        Response.AddHeader("Content-Disposition", "Attachment;FileName=");
        Response.ContentType = "APPLICATION/octet-stream";
        Response.BinaryWrite(net.DownloadData(URL));
        Response.End();
    }

0 个答案:

没有答案