Missing header in binary file representation of Powerpoint

Here’s my code in an asp.net web service. I am trying to return a powerpoint to the client.
Dim file As FileStream = New FileStream(“test.ppt”, FileMode.Create, FileAccess.ReadWrite)
pres.Write(file)
Dim docbinaryarray() As Byte
ReDim docbinaryarray(file.Length)
file.Read(docbinaryarray, 0, docbinaryarray.Length)
file.Close()
Return docbinaryarray

When I try to consume this web service in an asp.net web application, I attempt to read in the returned array of bytes. However, the client complains that the header of the file is missing and does not recognize the binary stream as a powerpoint file.

Any ideas? Thank you.

Hello simone_06.
After writing of presentation, filestream’s position points to the end of file. Place
file.Position() = 0
before file.Read(…).

I have added the file.position = 0 to the web service. I’m still getting this error when I try to open the ppt presentation: PowerPoint can’t open the type of file represented by c:\test.ppt.

Here is my code for consuming the web service:

Dim docbinaryarray() As Byte
docbinaryarray = cp.CreatePowerpoint() 'The call to the web service
Dim file As System.IO.FileStream = New System.IO.FileStream(“test.ppt”, FileMode.Create, FileAccess.ReadWrite)
file.Write(docbinaryarray, 0, docbinaryarray.Length)

The docbinaryarray.length is not 0. There appears to be data in the array.

Thank you.

Dear simone_06.
You are right, there is another one mistake I haven’t noticed. Arrays definition in VB requires not size but upper bound specification, so “docbinary” array after function is one byte longer than file test.ppt. Change function to



Dim file As FileStream = New FileStream(“test.ppt”, FileMode.Create, FileAccess.ReadWrite)
pres.Write(file)
Dim docbinaryarray() As Byte = Nothing
If (file.Length > 0) Then
ReDim docbinaryarray(file.Length - 1)
file.Position = 0
file.Read(docbinaryarray, 0, docbinaryarray.Length)
End If
file.Close()
Return docbinaryarray

or, in case you don’t need writing of test.ppt to disk better use MemoryStream:


Dim stream As MemoryStream = New MemoryStream
pres.Write(stream)
Dim size As Long = stream.Length
stream.Close()
Dim docbinaryarray() As Byte = stream.GetBuffer()
ReDim Preserve docbinaryarray(size - 1)
Return docbinaryarray



I think we’re getting closer. I have changed the web service code to what you have recommended using MemoryStream. Thanks. Now back to the asp.net web application that is attempting to consume the web service. Again, here is my code:
Dim docbinaryarray() As Byte
docbinaryarray = cp.CreatePowerpoint()
Dim file As System.IO.FileStream = New System.IO.FileStream(“c:\inetpub\wwwroot\asposewebtest\test.ppt”, FileMode.Create, FileAccess.ReadWrite)
file.Position() = 0
file.Write(docbinaryarray, 0, docbinaryarray.Length - 1)

I’m still getting this error when I try to open the ppt presentation that is created. It is definitely creating the test file but Powerpoint doesn’t want to open it: “PowerPoint can’t open the type of file represented by c:\test.ppt.” Also, the length of the array is definitely greater than 0. There appears to be a repackaging problem from array to file.


simone_06, after you changed cp.CreatePowerPoint() code, docbinaryarray is no longer corrupt, so there is no need to subtract 1 from docbinaryarray.Length.
Change “file.Write(docbinaryarray, 0, docbinaryarray.Length - 1)” to “file.Write(docbinaryarray, 0, docbinaryarray.Length)” and all should work fine.
At least following code works (it is a part from simple window application):


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim file As FileStream = New FileStream(“out.ppt”, FileMode.Create, FileAccess.ReadWrite)
Dim bytearray() As Byte = LoadSavePres()
file.Write(bytearray, 0, bytearray.Length)
file.Close()
End Sub

Private Function LoadSavePres()
Dim pres As Presentation = New Presentation(“demo.ppt”)
Dim stream As MemoryStream = New MemoryStream
pres.Write(stream)
Dim size As Long = stream.Length
stream.Close()
Dim docbinaryarray() As Byte = stream.GetBuffer()
ReDim Preserve docbinaryarray(size - 1)
Return docbinaryarray
End Function


I have adjusted my code as you recommended. I get the same error message. I did a little test by adding code to the consumer to get the file into a presentation. The error I get on the last line below is: Unable to read entire header; 0 bytes read; expected 512 bytes.

Dim docbinaryarray() As Byte
docbinaryarray = cp.CreatePowerpoint()
Dim file As System.IO.FileStream = New System.IO.FileStream(“c:\inetpub\wwwroot\asposewebtest\test.ppt”, FileMode.Create, FileAccess.ReadWrite)
file.Position() = 0
file.Write(docbinaryarray, 0, docbinaryarray.Length)
Dim pres As Presentation = New Presentation(file)

simone_06 wrote:

Dim docbinaryarray() As Byte
docbinaryarray = cp.CreatePowerpoint()
Dim file As System.IO.FileStream = New System.IO.FileStream("c:\inetpub\wwwroot\asposewebtest\test.ppt", FileMode.Create, FileAccess.ReadWrite)
file.Position() = 0
file.Write(docbinaryarray, 0, docbinaryarray.Length)
Dim pres As Presentation = New Presentation(file)

This code won't work. After you write to file, it's current position points to docbinaryarray.Length byte, in our case - to end of file position. Presentation constructor doesn't rewind stream to the beginning of file, so it won't be able to read anything from it. Move "file.Position = 0" to after writing but before passing "file" to presentation constructor.

If there're an other error, please check if written file "c:\inetpub\wwwroot\asposewebtest\test.ppt" can be opened with MS PowerPoint and some of Aspose.PowerPoint sample application. Make sure MemoryStream in CreatePowerpoint() function before closing, docbinaryarray before writing and file stream all have same length, same first and last bytes.

Nope, test.ppt won’t open in Powerpoint. MemoryStream does not have same length as docbinaryarray.

They should be equal. Maybe you forget to redim array, returned by MemoryStream.GetBuffer() (usualy it a bit longer than MemoryStream itself) or do not subtract one from data size, when pass it to Dim or Redim statement?

I’m sorry about all of the posts, but I need to get this to work, or my company will not purchase these products. We have to be able to use these components in web services.

Here is all of my code:

Web Service:

Dim stream As MemoryStream = New MemoryStream
pres.Write(stream)
Dim size As Long = stream.Length
stream.Position = 0
Dim docbinaryarray() As Byte
ReDim Preserve docbinaryarray(size - 1)
docbinaryarray = stream.GetBuffer()
Return docbinaryarray

**MemoryStream size = 161280
**BinaryArray size = 262144

Consumer:

Dim docbinaryarray() As Byte
docbinaryarray = cp.CreatePowerpoint()
Dim file As System.IO.FileStream = New System.IO.FileStream(“c:\inetpub\wwwroot\asposewebtest\test.ppt”, FileMode.Create, FileAccess.ReadWrite)
file.Write(docbinaryarray, 0, docbinaryarray.Length)

**test.ppt is created but will not open


Simone_06, in web service code you should obtain docbinaryarray from memoryStream (“MemoryStream.GetBuffer()”), and then change it’s size using “Redim preserve” statement.
Web service should be:



Dim stream As MemoryStream = New MemoryStream
pres.Write(stream)
Dim size As Long = stream.Length 'remembering size as it unavailable after Close() call
stream.Close()
Dim docbinaryarray() As Byte
docbinaryarray = stream.GetBuffer() ’ getting stream’s buffer
ReDim Preserve docbinaryarray(size - 1) 'changing size
Return docbinaryarray


That worked!!! Thank you so much!!