title: Problem with download files from databases through GridView
body: <p>I have problem with files. I'm trying to download files from databases 
  through gridview, but after I download them, they are with wrong name and extensions. Their name are my web form name and their extensions are <code>.aspx</code>.</p>
  
  <p>In the database they are saved in the correct format.</p>
  
  <p>Here are my code-behind</p>
  
  <pre><code>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 &gt; 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);
          }
      }
  }
  </code></pre>
  
  <p>and my html</p>
  
  <pre><code> &lt;form id="form1" runat="server"&gt;
      &lt;div&gt;
          &lt;table&gt;
              &lt;tr&gt;
                  &lt;td&gt;
                      &lt;asp:Label ID="Label1" runat="server" Text="Document"&gt;&lt;/asp:Label&gt;
                      &lt;asp:FileUpload ID="FileUpload1" runat="server" /&gt;
                      &lt;asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" /&gt;
                      &lt;br /&gt;
                  &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
          &lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"&gt;
              &lt;Columns&gt;
                  &lt;asp:TemplateField HeaderText="Documents"&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:LinkButton ID="LinkButton1" OnClick="OpenDocument" runat="server" Text='&lt;%# Eval("name") %&gt;'&gt;&lt;/asp:LinkButton&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
              &lt;/Columns&gt;
          &lt;/asp:GridView&gt;
          &lt;br /&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Aphm1.png" rel="nofollow noreferrer">img</a></p>