Recrée un fichier (.PDF,.DOC,.XLS...) depuis une table fichier (contenu de type

Résolu
issamesisa Messages postés 30 Date d'inscription jeudi 8 mai 2008 Statut Membre Dernière intervention 15 septembre 2010 - 13 sept. 2010 à 14:08
issamesisa Messages postés 30 Date d'inscription jeudi 8 mai 2008 Statut Membre Dernière intervention 15 septembre 2010 - 15 sept. 2010 à 10:46
Bonjour,
je veux recrée des fichiers qui sont stocké dans ma base donnée sous sql server 2005, qui contient
 TABLE [fichiers]
(
         [ID] [int] IDENTITY(1,1) NOT NULL,
         [FileName] [nvarchar](15) NOT NULL,
         [Extension] [nvarchar](5) NOT NULL,
         [Content] [varbinary(max)] NULL
) 


l'ajout a la base de donnée et la conversion depui, par exemple ,fichier .doc a un donnée binaire a la table sa marche bien
maintenant je veux recrée le fichier en cliquant sur le fichier qui est dans une grid view
Merci

2 réponses

issamesisa Messages postés 30 Date d'inscription jeudi 8 mai 2008 Statut Membre Dernière intervention 15 septembre 2010
14 sept. 2010 à 16:43
C bon j'ai trouver de solution , il me restes pour les fichiers .docx (word 2007)
voila la solution pour (PDF/XLS/XLSX/JPG/PNG/GIF)

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();
    }
}
3
issamesisa Messages postés 30 Date d'inscription jeudi 8 mai 2008 Statut Membre Dernière intervention 15 septembre 2010
15 sept. 2010 à 10:46
Solution final
=> pour tout type de fichier
telechargement des fichies depuis une base de donnée de fichier de type binaire converti
Premiere tache était (pdf/doc/docx/xls/xlsx/jpg/png/gif) vert base donnée en sql server 2005 vers un type varbinary(max) pour le contenue des fichiers
et maintnante la solution pour les télécharger depuit la base de donnée
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());
        }
    }
    
}
3
Rejoignez-nous