vendredi 24 avril 2020

Problem with download files from databases through GriedView

I have problem with files.Im trying to download files from databases through GriedView ,but after i download them they are with wrong name and extensions .Their name are my web form name and Their extensions are aspx. in the database they are saved in the correct format.

here are my code-behind

 public partial class data : System.Web.UI.Page
{
    string cs = ConfigurationManager.ConnectionStrings["DecumentsConnectionString"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillData();
        }
    }

    private void FillData()
    {
        DataTable dt = new DataTable();
        using (SqlConnection cn = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("getDecuments", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cn.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            dt.Load(reader);
            if (dt.Rows.Count > 0)
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        FileInfo filename = new FileInfo(FileUpload1.FileName);
        byte[] documentContent = FileUpload1.FileBytes;
        string name = filename.Name;
        string extnt = filename.Extension;

        using (SqlConnection con = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("savedocument", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@name", name);
            cmd.Parameters.AddWithValue("@Content", documentContent);
            cmd.Parameters.AddWithValue("@extn",extnt);
            con.Open();
            cmd.ExecuteNonQuery();
        }
    }
    private void Donwload(int id)
    {
        DataTable dt = new DataTable();
        using (SqlConnection cn = new SqlConnection(cs))
        {
            SqlCommand cmd = new SqlCommand("getdocument", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@ID", id);
            cn.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            dt.Load(reader);
        }
        string name = dt.Rows[0]["name"].ToString();
        byte[] document = (byte[])dt.Rows[0]["DocumentContent"];
        Response.ClearContent();
        Response.ContentType = "application/octetstream";
        Response.AddHeader("Content-Dispisition", string.Format("attachment;filename={0}", name));
        Response.AddHeader("Content-Lenght", document.Length.ToString());
        Response.BinaryWrite(document);
        Response.Flush();
        Response.Close();
    }
        protected void OpenDocument(object sender, EventArgs e)
        {
            LinkButton lnk = (LinkButton)sender;
            GridViewRow gr = (GridViewRow)lnk.NamingContainer;
            int id = int.Parse(GridView1.DataKeys[gr.RowIndex].Value.ToString());
            Donwload(id);
        }
    }
}

and my html

 <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Document"></asp:Label>
                    <asp:FileUpload ID="FileUpload1" runat="server" />
                    <asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" />
                    <br />
                </td>
            </tr>
        </table>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID">
            <Columns>
                <asp:TemplateField HeaderText="Documents">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" OnClick="OpenDocument" runat="server" Text='<%# Eval("name") %>'></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <br />
    </div>
</form>

img




Aucun commentaire:

Enregistrer un commentaire