Saving document to stream using docx format

Hello, I am trying to save a document to a stream using the Aspose.Words version 5.2.2. I am using the following command:

doc.Save(stream, SaveFormat.Docx);
byte[] output = stream.GetBuffer();

I then read my byte array into word and get an error saying the file is corrupt. If I use SaveFormat.Doc then I am able to open the document but we would like to save our merge output in the new docx format.

Any help is appreciated, Ryan

Hi
Thanks for your request. How to you read your byte array into word? Could you please provide me code example that will allow me to reproduce the problem? Also please try using the latest version of Aspose.Words (6.0). You can download it from here:
https://releases.aspose.com/words/net
Best regards.

Sure, we pass the byte array to the following function:

context.Response.Clear();
context.Response.ClearContent();
context.Response.ClearHeaders();

// Buffer response so that page is sent
// after processing is complete.
context.Response.BufferOutput = true;

string contentType = string.Empty;
string contentDisposition = string.Empty;

switch (corrItemTypeId)
{
    case (int)CorrItemTypeEnum.Image:
        contentType = "application/octet-stream";
        contentDisposition = "inline";
        break;

    default:
        contentType = "application/msword";
        contentDisposition = "attachment";
        break;
}
context.Response.AddHeader("Content-Disposition", string.Format("{0};filename={1}.{2}", contentDisposition, fileName, fileExt));

// Add the file size into the response header
context.Response.AddHeader("Content-Length", data.Length.ToString());

using (MemoryStream ms = new MemoryStream(data))
{
    long dataLengthToRead = ms.Length;
    int blockSize = dataLengthToRead >= 5000 ? 5000 : (int)dataLengthToRead;
    byte[] buffer = new byte[dataLengthToRead];

    // Write the document into the response
    while (dataLengthToRead > 0 && context.Response.IsClientConnected)
    {
        Int32 lengthRead = ms.Read(buffer, 0, blockSize);
        context.Response.OutputStream.Write(buffer, 0, lengthRead);
        dataLengthToRead = dataLengthToRead - lengthRead;
        // do not flush since BufferOutput = true
    }

    context.Response.Flush();
    context.Response.Close();
}

Again this works fine for SaveFormat.Doc but then the file is not opened properly in word2007 with the docx file extension. Thanks and I will try the newest version of the dll.

Hi

Thanks for your request. I think that you should call Respons.End() after saving document. Please try using the following code.

Document doc = new Document(Server.MapPath("in.docx"));
doc.Save("out.docx", SaveFormat.Docx, SaveType.OpenInWord, Response);
Response.End();

Hope this helps.
Best regards.

Couple things, I am trying to save the document to a stream. Saving it to a file works fine for me but passing the save command a stream is where I’m running into the issue. Also I am saving the document in a service class so Response.End() is not available. The second piece of code I sent is contained in the asp c# code but this calls into the service to handle the mail merge. I have also tried version 6.0 and am getting the same error.

Thanks

context.Response.Clear();
context.Response.ClearHeaders();

// Buffer response so that page is sent
// after processing is complete.
context.Response.BufferOutput = true;

string contentType = string.Empty;
string contentDisposition = string.Empty;

switch (corrItemTypeId)
{
    case (int)CorrItemTypeEnum.Image:
        contentType = "application/octet-stream";
        contentDisposition = "inline";
        break;

    default:
        contentType = "application/msword";
        contentDisposition = "attachment";
        break;
}
context.Response.AddHeader("Content-Disposition", string.Format("{0};filename={1}.{2}", contentDisposition, fileName, fileExt));

// Add the file size into the response header
context.Response.AddHeader("Content-Length", data.Length.ToString());
// No need for pumping since holding byte[] data in memory anyway
Context.Response.Write(data, 0, data.Length);
Context.Response.End();

Hope it helps.

Hi
I had seen such problem with DOCX files before. Response.End() method should resolve this problem. In your case you should use context.Response.End().
Please let me know in case of any issues.
Best regards.