none
如何跟踪用户是否真的下载了 RRS feed

  • 问题

  • using System;
    using System.Web;
    using System.IO;
    using System.Text;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //参照http://www.codeproject.com/Articles/74654/File-Download-in-ASP-NET-and-Tracking-the-Status-o
        }
    
        //Function for File Download in ASP.Net in C# and 
        //Tracking the status of success/failure of Download.
        private bool DownloadableProduct_Tracking()
        {
            //File Path and File Name
            string filePath = Server.MapPath("~/Download/");
            string fileName = "fjslapp4.0.0.ipa";
    
            FileInfo fi = new FileInfo(filePath + fileName);
            FileStream fs = new FileStream(filePath + fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    
            //Reads file as binary values
            BinaryReader br = new BinaryReader(fs);
    
            //Ckeck whether user is eligible to download the file
            if (IsEligibleUser())
            {
                //Check whether file exists in specified location
                if (fi.Exists)
                {
                    try
                    {
                        long startBytes = 0;
                        string lastUpdateTimeStamp = File.GetLastWriteTimeUtc(filePath).ToString("r");
                        string encodeData = HttpUtility.UrlEncode(fileName, Encoding.UTF8) + lastUpdateTimeStamp;
    
                        Response.Clear();
                        Response.Buffer = false;
                        Response.AddHeader("Accept-Ranges", "bytes");
                        Response.AppendHeader("ETag", "\"" + encodeData + "\"");
                        Response.AppendHeader("Last-Modified", lastUpdateTimeStamp);
                        //Set the ContentType
                        Response.ContentType = "application/octet-stream";
                        //Add the file name and attachment, 
                        //which will force the open/cancel/save dialog to show, to the header
                        Response.AddHeader("Content-Disposition", "attachment;filename=" + fi.Name);
                        //Add the file size into the response header
                        Response.AddHeader("Content-Length", (fi.Length - startBytes).ToString());
                        Response.AddHeader("Connection", "Keep-Alive");
                        //Set the Content Encoding type
                        Response.ContentEncoding = Encoding.UTF8;
    
                        //Send data
                        br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
    
                        //Dividing the data in 1024 bytes package
                        int maxCount = (int)Math.Ceiling((fi.Length - startBytes + 0.0) / 1024);
    
                        //Download in block of 1024 bytes
                        int i;
                        for (i = 0; i < maxCount && Response.IsClientConnected; i++)
                        {
                            Response.BinaryWrite(br.ReadBytes(1024));
                            Response.Flush();
                        }
                        //if blocks transfered not equals total number of blocks
                        if (i < maxCount)
                        {
                            WriteTxt(Server.MapPath("~/Download/log.txt"), "未下载完");
                            return false;
                        }
                        WriteTxt(Server.MapPath("~/Download/log.txt"), "已下载");
                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                    finally
                    {
                        Response.End();
                        //Close Binary reader and File stream
                        br.Close();
                        fs.Close();
                    }
                }
                else System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(),
                    "FileNotFoundWarning", "alert('File is not available now!')", true);
            }
            else
            {
                System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(),
                    "NotEligibleWarning", "alert('Sorry! File is not available for you')", true);
            }
            return false;
        }
    
        private bool IsEligibleUser()
        {
            return true;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            DownloadableProduct_Tracking();
        }
        private void WriteTxt(string fileFullName,string strContent)
        {
            StreamWriter sw;
            //不存在
            if (!File.Exists(fileFullName))
            {
                sw = File.CreateText(fileFullName);
            }
            else
            {
                sw = File.AppendText(fileFullName);
            }
            try
            {
                sw.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + strContent);
                sw.Write(System.Environment.NewLine);
            }
            catch (Exception ex)
            {
                
            }
            sw.Close();
        }
    }
    以上实例没有达到效果

    开心了就笑,不开心了就过会儿再笑

    2013年12月25日 6:50

答案