using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class affichage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { try { int PictureID = Convert.ToInt32(Request.QueryString["id_fich"]); using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["GestionfichiersqliConnectionString2"].ConnectionString)) { const string SQL "SELECT [typecontenu], [contenu] FROM [fichiers] WHERE [id_fich] @ID"; SqlCommand myCommand = new SqlCommand(SQL, myConnection); myCommand.Parameters.AddWithValue("@ID", 4); myConnection.Open(); SqlDataReader myReader = myCommand.ExecuteReader(); if (myReader.Read()) { string strResponse.ContentType myReader["typecontenu"].ToString(); if (str == "image/jpg") { SqlCommand cmd new SqlCommand("SELECT [nom_fiche],[typecontenu], [contenu] FROM [fichiers] WHERE [id_fich] @ID"); cmd.Parameters.Add("@ID", SqlDbType.Int).Value = 1; DataTable dt = GetData(cmd); if (dt != null) { download(dt); } } else Response.BinaryWrite((byte[]) myReader["contenu"]); } myReader.Close(); myConnection.Close(); } } catch (Exception ex) { Response.Write(ex.ToString()); } } private DataTable GetData(SqlCommand cmd) { DataTable dt = new DataTable(); String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["GestionfichiersqliConnectionString3"].ConnectionString; SqlConnection con = new SqlConnection(strConnString); SqlDataAdapter sda = new SqlDataAdapter(); cmd.CommandType = CommandType.Text; cmd.Connection = con; try { con.Open(); sda.SelectCommand = cmd; sda.Fill(dt); return dt; } catch { return null; } finally { con.Close(); sda.Dispose(); con.Dispose(); } } private void download(DataTable dt) { Byte[] bytes = (Byte[])dt.Rows[0]["contenu"]; string strr = (string)dt.Rows[0]["nom_fiche"]; string []chaine = (string[])strr.Split('.'); string machaine = chaine[0]; Response.Buffer = true; Response.Charset = ""; Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ContentType = "application/vnd.ms-word.document.12"; //dt.Rows[0]["typecontenu"].ToString(); Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AddHeader("content-disposition", "attachment;filename=" + machaine + ".docx"); Response.ClearContent(); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); } }
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.IO; using System.Drawing.Imaging; using System.Drawing; public partial class affichage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { try { //int PictureID = Convert.ToInt32(Request.QueryString["id_fich"]); using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["GestionfichiersqliConnectionString2"].ConnectionString)) { const string SQL "SELECT [nom_fiche],[typecontenu], [contenu] FROM [fichiers] WHERE [id_fich] @ID"; SqlCommand myCommand = new SqlCommand(SQL, myConnection); myCommand.Parameters.AddWithValue("@ID", 14); myConnection.Open(); SqlDataReader myReader = myCommand.ExecuteReader(); if (myReader.Read()) { string strResponse.ContentType myReader["typecontenu"].ToString(); byte[] contenu = (byte[])(myReader [2]); Response.ContentType = myReader [1].ToString(); Response.AddHeader("Content-Disposition", "attachment;filename=" + myReader[0]); Context.Response.BinaryWrite(contenu); } myReader.Close(); myConnection.Close(); } } catch (Exception ex) { Response.Write(ex.ToString()); } } }