Pages

Thursday, 22 March 2012

How to Download a file from the Data Base


//First find the File name from the data base then only it will work

protected void imgbtnDwnld_Click(object sender, ImageClickEventArgs e)
    {
        string strSId = ((ImageButton)sender).Attributes["Filename"].ToString().Trim();
     
        string filename = Server.MapPath(strSId.ToString());
        FileInfo fileInf = new FileInfo(filename);
        if (fileInf.Exists)
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", "inline;attachment; filename=" + fileInf.Name);
            Response.AddHeader("Content-Length", fileInf.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.Flush();
            Response.TransmitFile(fileInf.FullName);
            Response.End();
        }
        else
        {
            ShowActionMessage("Sorry unable to display the price list right now.");
        }

    }


 private void ShowActionMessage(string Message)
    {
        string message = Message;

        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        sb.Append("<script type = 'text/javascript'>");

        sb.Append("window.onload=function(){");

        sb.Append("alert('");

        sb.Append(message);

        sb.Append("')};");

        sb.Append("</script>");

        ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
    }

No comments:

Post a Comment