When using the DocX librairy, i am generating the docx document on server then download it.
For that, i need to convert my document in an array of bytes.
To do that, i was previousloy saving the document as a physical file like this :
// Save all changes to this document.
document.SaveAs(GENERATED_DOCX_LOCATION);
return System.IO.File.ReadAllBytes(GENERATED_DOCX_LOCATION);
but i would rather not do that. Is it possible to serialize this object to download it without saving it physically ?
I already tried that :
private byte[] ObjectToByteArray(object obj)
{
if (obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
bf.Serialize(ms, obj);
return ms.ToArray();
}
}
With :
return this.ObjectToByteArray(document);
But obviously, DocX doesn't implement ISerializable.
EDIT : the code below doesn't work either
byte[] byteArray = null;
using (var stream = new MemoryStream())
{
document.SaveAs(stream);
byteArray = stream.ToArray();
}
return byteArray;