Bonjour,

J'utilise le composant UpFileLoad pour télécharger un fichier et enregistrer différentes informations le concernant dans la base de données au moyen des instructions suivantes :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
command.CommandText = "INSERT INTO TableImage(Image,Chemin,Taille,DateCreation,ContentType) values (@file_bytes,@Chemin,@Taille,@DateCreation,@ContentType)";
                                command.Parameters.Add("file_bytes", SqlDbType.VarBinary).Value = FileUpload1.FileBytes;
                                command.Parameters.Add("Chemin", SqlDbType.VarChar, 50).Value = FileUpload1.FileName;
                                command.Parameters.Add("Taille", SqlDbType.VarChar, 50).Value = FileUpload1.PostedFile.ContentLength;
                                command.Parameters.Add("DateCreation", SqlDbType.Date).Value = DateTime.Now.ToString();
                                command.Parameters.Add("ContentType", SqlDbType.VarChar, 50).Value = FileUpload1.PostedFile.ContentType;
J'ai remarqué que lorsque j'enregistre un fichier .docx (word 2007), j'obtiens le type MIME suivant :
application/vnd.openxmlformats-officedocument.word

Je rencontre alors un problème (seulement pour les documents docx) lorsque je veux télécharger le fichier chez le client car j'obtiens un message d'erreur au niveau de l'encodage des caractères :

Une partie de mon code pour télécharger le document chez le client :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
DataTable tableFichier = new DataTable();
            using (SqlConnection cnDataBase = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
            {
                cnDataBase.Open();
                SqlCommand cmTable = new SqlCommand("select * from TableImage where Chemin=@CheminFichier", cnDataBase);
 
                cmTable.Parameters.Add("@CheminFichier", SqlDbType.VarChar, 50);
                cmTable.Parameters["@CheminFichier"].Value = Nomfich;
 
                SqlDataAdapter daTable = new SqlDataAdapter(cmTable);
                daTable.Fill(tableFichier);
            }
 
          if (tableFichier.Rows.Count > 0)
            {
               Response.Clear();
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                Response.ContentType = tableFichier.Rows[0]["ContentType"].ToString();
                Response.OutputStream.Write((byte[])tableFichier.Rows[0]["Image"], 0, Convert.ToInt32(tableFichier.Rows[0]["Taille"]));
                Response.End()};
J'ai remarqué que si je teste avec le code ci-dessous, le document docx arrive à s'ouvrir :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
Context.Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
Comment pourrais-je alors faire pour obtenir le bon type MIME lors de l'enregistrement dans ma base de données qui me permettrait d'ouvrir mon document ?

Merci.