下载对话框的实现

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

你可能感兴趣的:(exception,String,buffer,Path,byte,dataset)