In the previous question in the article that I suggest I explain how to download a file
[ASP.NET] Burn file on base of data
If you analyze the data layer you will see the method to recover a file by the id.
public static byte[] GetById(int Id)
{
using (MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["default"].ToString()))
{
conn.Open();
string query = @"SELECT archivo
FROM Archivos
WHERE Id = @id";
MySqlCommand cmd = new MySqlCommand(query, conn);
cmd.Parameters.AddWithValue("@id", Id);
MySqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
return (byte[])reader["archivo"];
}
}
return null;
}
The important thing is to see how the content of the field is assigned as byte[]
in the line of the return
With that result you should send it in Response
to the client for download. There are several ways to achieve the download but the simplest is through a page that could be called download.aspx
public partial class Download : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int id = Convert.ToInt32(Request.QueryString["id"]);
byte[] archivo = ArchivosDAL.GetById(id);
Response.Clear();
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", "nombrearch.pdf"));
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(archivo.ContenidoArchivo);
Response.End();
}
}
The ContentType = "application/octet-stream"
is defined to show the download box instead of showing the pdf embedded in the browser