Free Support Forum - aspose.com

Error loading a presentation from an IO.Stream - Urgent help required

Hello, please can you advise on how we may create a new presentation using the content of a Memory Stream as apposed to a File Path? We are obtaining a PPT file over HTTP which we then need to use to populate a new Aspose Presentation object thus...

Dim wr As HttpWebRequest = CType(WebRequest.Create(strPath), HttpWebRequest)
Dim ws As HttpWebResponse = CType(wr.GetResponse(), HttpWebResponse)
Dim str As System.IO.Stream = ws.GetResponseStream()

Dim inBuf(10000000) As Byte
Dim bytesToRead As Integer = CInt(inBuf.Length)
Dim bytesRead As Integer = 0
While bytesToRead > 0
Dim n As Integer = str.Read(inBuf, bytesRead, bytesToRead)
If n = 0 Then
Exit While
End If
bytesRead += n
bytesToRead -= n
End While

presSource = New Presentation(str)

The problem with this however is that the following exception occurs on the last line of this code segment:

Message "Unable to read entire header; 0 bytes read; expected 512 bytes" String

Thanks in advance.

Allan Ashton (aashton@ey.uk.com)

How Aspose.Slides can read presentation from the stream if you already read
all information from this stream to “inBuf” ?
Save position and change it back before opening presentation.

I still get the same error having removed the code to load the content into the inBuf byte array.

Dim wr As HttpWebRequest = CType(WebRequest.Create(strPath), HttpWebRequest)
Dim ws As HttpWebResponse = CType(wr.GetResponse(), HttpWebResponse)
Dim str As System.IO.Stream = ws.GetResponseStream()

presSource = New Presentation(str)

Please can you provide me with an example on how to populate a Presentation using a Stream as I am attempting to do here?

Are you sure responce stream contains presentation in ppt format?
Try to save all information from response stream to a file and check if it can be opened by MS PowerPoint.

Also you can try to use MemoryStream and open presentation from it.

                Dim wr As HttpWebRequest = CType(WebRequest.Create(strPath), HttpWebRequest)
Dim ws As HttpWebResponse = CType(wr.GetResponse(), HttpWebResponse)
Dim str As System.IO.Stream = ws.GetResponseStream()

Dim ms As MemoryStream = New MemoryStream()
Dim buf(4096) As Byte

While True
Dim bytesRead As Integer = str.Read(buf, 0, buf.Length);
If bytesRead <= 0
Exit While
End If
ms.Write(buf, 0, bytesRead);
End While

ms.Position = 0

presSource = New Presentation(ms)

Also check if this VB code is correct. I just converted it from C#.

That worked! Thanks a bunch.Smile [:)]

I'm seeing the exact same problem (IO exception when trying to construct a presentation from a memorystream, works from a file path). Can anyone from Aspose comment on this? Saving the data to a file is not an option.

Jeff Amfahr

stream.Position = 0

before Presentation constructor call.

That didn't change the behavior. The exact error I'm getting is

An exception of type 'System.IO.IOException' occurred in Aspose.Slides.DLL but was not handled in user code

Additional information: Unable to read entire block; 1 byte read; expected 512 bytes

The code is similiar to below

..BLOBData is a Byte array that I get from a database. It is valid ppt file.

Using blobStream As New System.IO.MemoryStream(BLOBData)

blobStream.Position = 0

Dim slideDoc As New Aspose.Slides.Presentation(blobStream)

Ok, can you put BLOBData byte array to a binary file and send to me?

I have attached the file. This was generated by modify the actual code to be. Note if you change the file extension to ppt it opens in PowerPoint normally. Thanks for your help.

Using blobStream As New System.IO.MemoryStream(BLOBData)

Dim foo As New IO.FileStream("D:\Test.out", IO.FileMode.Create)

foo.Write(BLOBData, 0, blob.FileSize)

foo.Close()

blobStream.Position = 0

Dim slideDoc As New Aspose.Slides.Presentation(blobStream)

I have found a workaround. If instead of

Dim blobStream As New System.IO.MemoryStream(BLOBData)

I write

Dim blobStream As New System.IO.MemoryStream()

blobStream .Write(BLOBData, 0, FileSize)

Then Aspose.Slides can read the memory stream. I'm using the first construct for Aspose.Words without any problem.

So probably your BLOBData array contains also some garbage information together with real ppt file.
For correct work stream should have only ppt and nothing else.