下载对话框的实现

来源:互联网 发布:网络运营师要考证吗 编辑:程序博客网 时间:2024/05/23 19:15

protected void lbtnDownload_Command(object sender, CommandEventArgs e)
    {
        BllDYPolicy bll = new BllDYPolicy();
        int fileId = Convert.ToInt32(e.CommandArgument);
        bool isSuccess = true;      
        DataSet ds = bll.GetList(fileId);
        string filepath = ds.Tables[0].Rows[0]["filepath"].ToString();
        string filesourcename = ds.Tables[0].Rows[0]["filesourcename"].ToString();
        string extensionName = filepath.Substring(filepath.LastIndexOf("."));
        string fileName = String.Format("attachment; filename=/"{0}{1}/"", HttpUtility.UrlEncode(filesourcename), extensionName);
        string path = Server.MapPath(this._mainPath + filepath);

        try
        {
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", fileName);
            Response.AddHeader("Content-Length", fs.Length.ToString());
            byte[] buffer = new byte[fs.Length];
            fs.Read(buffer, 0, buffer.Length);
            Response.BinaryWrite(buffer);
        }
        catch (Exception ex)
        {
            isSuccess = false;
            Context.Response.ClearHeaders();
            Context.Response.Clear();
            Context.Response.ContentType = "text/html";
            //Context.Response.Write("<script>alert('" + ex.Message + "');</script>");
            JSUtility.Alert(ex.Message);
        }
        if (isSuccess) Context.Response.End();
    }

原创粉丝点击