Générer le téléchargement d'un fichier docx
Bonjour
Je voudrais générer le téléchargement d'un fichier docx, en modifiant, bien entendu, le response de la page. J'utilise le SDK Open XML V2.
Le problème est que cela génère une erreur quand je change le response, une erreur de parsing XML
Citation:
Error parsing near '<?xml version="1.0"
. Normal donc, puisqu'il n'arrive pas à renvoyer le docx car il trouve un doc xml... Alors je ne vois pas trop comment faire.
Je me suis inspiré de cet Article :http://internsp.blogspot.com/2007/10...ocx-files.html
Voici mon code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
//On instancie un WordProcessingDocument à partir d'un fichier
this.WordProcessingDocument =WordprocessingDocument.Open("C:\Mondoc.docx", true);
byte[] outputBytes = new byte[this.WordProcessingDocument.MainDocumentPart.GetStream().Length];
//On récupère le stream de la partie principale
this.WordProcessingDocument.MainDocumentPart.GetStream().Read(outputBytes, 0, outputBytes.Length);
memoryStream = new MemoryStream(outputBytes);
//Changement des headers
this.PageForDownload.Response.ClearHeaders();
this.PageForDownload.Response.AddHeader("content-disposition", "attachment; filename=test.docx");
this.PageForDownload.Response.ClearContent();
this.PageForDownload.Response.ContentEncoding = System.Text.Encoding.UTF8;
this.PageForDownload.Response.ContentType = "application/vnd.ms-word.document.12";
memoryStream.Position = 0;
BinaryWriter writer = new BinaryWriter(this.PageForDownload.Response.OutputStream);
BinaryReader reader = new BinaryReader(memoryStream);
writer.Write(reader.ReadBytes((int)memoryStream.Length));
reader.Close();
writer.Close();
memoryStream.Close();
this.PageForDownload.Response.Flush();
this.PageForDownload.Response.Close(); |
Merci de votre aide.