This is a C# code snippet that handles the download button click event in a web application. The code first retrieves the news ID from a hidden field and calls a method to validate the user's access to download the resource. If the user has access, it then checks the type of resource being downloaded and performs the necessary actions. For example, if it's a paid resource, it checks if the user has enough points or 'U' currency to download it, and updates the user's account accordingly. Finally, it sets the file name and content type of the resource being downloaded and transmits it to the user's browser.

protected void down_Click(object sender, EventArgs e)
{
    string newsId = this.hnewsId.Value;
    GetNewsID(newsId);
    if (point != 0)
    {
        Validation(newsId);
        if (type == 10)
        {
            AddDownloadRecoed();//添加下载记录
            nn.UpdateClick(dtUser.Rows[0]["Id"].ToString(), 3);//修改校园通下载次数
            if (u.UserType == 3)
            {
                nn.UpdateClick(u.UserID.ToString(), 4);//修改子账号下载次数
            }
        }
        else if (type == 11)
        {
            //获取用户信息
            DataTable dtu = bllu.GetUser(u.UserID, u.UserName);
            double userPoint = 0;
            switch (pType)
            {
                case "普通点":
                    userPoint = Convert.ToDouble(dtu.Rows[0]["iPoint"]);
                    if (point > userPoint)//判断剩余点数是否足够下载
                    {
                        userPoint = Convert.ToDouble(dtu.Rows[0]["gPoint"]);
                        if ((point / 10) > userPoint)//判断剩余点数是否足够下载
                        {
                            ClientScript.RegisterStartupScript(this.GetType(), "", string.Format(@"<script type='text/javascript'>layer.alert('剩余点数不足,请先充值!'); </script>")); return;
                        }
                        else
                        {
                            UpdatePoint(2);//修改剩余U币
                            AddDownPoint(newsId, 0);
                        }
                    }
                    else
                    {
                        UpdatePoint(0);//修改剩余点数
                        AddDownPoint(newsId, 0);
                    }
                    break;
                case "U币":
                    userPoint = Convert.ToDouble(dtu.Rows[0]["gPoint"]);
                    if (point.ToString().ToUB1() > userPoint)//判断剩余U币是否足够下载
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", string.Format(@"<script type='text/javascript'>layer.alert('剩余U币不足,请先充值!'); </script>")); return;
                    }
                    UpdatePoint(1);//修改剩余U币
                    AddDownPoint(newsId, 1);
                    break;
                default:
                    break;
            }
            //添加下载记录
            string tablename = user_download();
            dm.AddUserDownload(u.UserName, newsId, tablename);
        }
        else if (type == 8 || type == 9)
        {

        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", string.Format(@"<script type='text/javascript'>layer.alert('下载失败!'); </script>")); return;
        }
    }
    //资源下载
    string fileName = dtNews.Rows[0]["NewsTitle"].ToString().Trim() + dtNews.Rows[0]["CabinetURL"].ToString().Trim().Substring(dtNews.Rows[0]["CabinetURL"].ToString().Trim().LastIndexOf("."));
    fileName = fileName.Replace("|", "").Replace("/", "").Replace(@"\", "").Replace("<", "").Replace(">", "").Replace("?", "").Replace(":", "").Replace("*", "");
    Response.ContentType = "application/x-zip-compressed";
    if (HttpContext.Current.Request.Browser.Browser != "Firefox")
    {
        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
    }
    else
    {
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
    }
    string filename = filePath;
    Response.TransmitFile(filename);
}
C# Download Button Click Event Handler: User Authentication and Resource Download

原文地址: https://www.cveoy.top/t/topic/npQT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录