Home:ALL Converter>How to download file from url and manage download count in c#

How to download file from url and manage download count in c#

Ask Time:2018-07-04T16:18:47         Author:Shailesh Chaudhary

Json Formatter

I have download.aspx page. In Page_Load event i download file from url and Increase download count and store to database.

When i refresh page download count increased. if i deny to download count also increased.

I want to increase count when i save file from browser.

My code is as following

    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();
    }

Author:Shailesh Chaudhary,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/51168956/how-to-download-file-from-url-and-manage-download-count-in-c-sharp
yy