We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Document corrupted when downloading from DB

Hi,

My code saves a Aspose.Word document to a MemoryStream, converts the stream to a Byte Array and then stores it in the database. Later, the user downloads the file, which performs the opposite: Database > Byte Array > Streamed to browser.

My problem: When they open the downloaded document, it's corrupted. See attached file.

Here's the code I use to save the document and convert it to a Memory Array. Note that if I save it to disk, it works fine:

Dim stream As New IO.MemoryStream

bodyDoc.Save(stream, Aspose.Words.SaveFormat.Doc)

'convert stream to byte array...

Dim length As Integer = stream.Length

Dim bytes(length) As Byte

stream.Read(bytes, 0, length)

'store in DB

Dim taFile As New Business.FileManagement.dsFileStoreTableAdapters.taQueries

Dim File_Id As Integer = CInt(taFile.InsertFileTransit(FileName, length, "application/msword", bytes, CurrentUser.AuditName))

Here's the code I use to download the document:

Dim si As New ServiceInterface.FileManagementSI

Dim dt As DataStructures.dsFileStore.FileDataTable = si.GetFile(File_Id)

Dim dr As DataStructures.dsFileStore.FileRow = dt(0)

Dim Response As System.Web.HttpResponse = HttpContext.Current.Response

Response.ClearContent()

Response.AppendHeader("content-length", result.Length.ToString())

Response.AppendHeader("content-disposition", String.Format("attachment;filename={0}", dr.filename))

Response.ContentType = dr.ContentType

Response.BinaryWrite(result)

Response.Flush()

Response.Close()

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. I think that you should use stream.GetBuffer() to convert Memorystream to byte array. For example see the following code.

'Create connction

Dim connectionString As String = "server=Web1;database=TestDB;uid=sa;pwd=dsa43r;"

Dim conn As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(connectionString)

' Open the DOC file using Aspose.Words.

If (fileuploadOpenFile.FileBytes.Length > 0) Then

Dim docStream As IO.Stream = fileuploadOpenFile.FileContent

Dim doc As Document = New Document(docStream)

' ...You can merge data/manipulate document content here.

Dim builder As DocumentBuilder = New DocumentBuilder(doc)

builder.Write("Edited by ASpose.Words")

Dim stream As IO.MemoryStream = New IO.MemoryStream()

'Save document to memorystream

doc.Save(stream, SaveFormat.Doc)

'Create sql command

Dim commandString As String = "INSERT INTO Documents (FileName, FileContent) VALUES(@Name, @Doc)"

Dim command As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(commandString, conn)

'Add paramenter @Doc

command.Parameters.AddWithValue("Doc", stream.GetBuffer())

command.Parameters.AddWithValue("Name", IO.Path.GetFileName(fileuploadOpenFile.FileName))

'Open connection

conn.Open()

'Write document to DB

command.ExecuteNonQuery()

'Close DB connection

conn.Close()

End If

Also I created a sample demo application for you. See attachment.

Best regards.