Create Web Service using Stream Save functionality

I have created a web service where I open a document, merge some fields, then save to a memorystream. This appears to work correctly.

From a client I call the web service and then try to repackage the stream and show it in a browers by using the following code

Dim word as new word
Dim doc as document = word.open(Stream)

doc.save(stream, Formatdocument, Openinbrowser, me.response)

it fails during this…

Are there any examples of code in VB during this type of thing?

Make sure you seek to the beginning of the memory stream before reading from it.

Roman - thanks for your quick response and help - here is more information regarding the situation we are experiencing…


WEB SERVICE CODE CREATED - this appears to created as expected
~

<WebMethod(Description:=“Run Report”)> _
Public Function ExecuteReport(ByVal psReportID As String) As Byte()

Dim word As Word = New Word
Dim doc As Document = word.Open(“C:\Test\Statement of Advice.doc”)
Dim loStream As New MemoryStream

'Fill the fields in the document with user data.
doc.MailMerge.Execute( _
New String() {“Report_Date”, “Plan_Name”}
New Object() {“17 June 2004”, "Fred’s New Plan}

doc.Save(loStream, SaveFormat.FormatDocument)

Return loStream.ToArray

End Function


CLIENT SIDE CODE CREATED - error occurs
~

Private Sub Button1_ServerClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ServerClick
'Dim cb As New AsyncCallback(AddressOf Me.callback1)
'Dim ar As IAsyncResult = svcs.BeginExecuteReport(cb, svcs)


Dim loArray As Byte()
Dim loProxy As New ExecuteReportProxy
Dim loStream As MemoryStream
Dim loDoc As Aspose.Word.Document
Dim loWord As New Aspose.Word.Word

loArray = loProxy.ExecuteReport(“”) - CALLING THE WEB SERVICE HERE

loStream = New MemoryStream(loArray)
loStream.Seek(0, SeekOrigin.Begin)
loDoc = loWord.Open(loStream) - ERROR OCCURS HERE

loDoc.Save(“Test”, SaveFormat.FormatDocument, SaveType.OpenInBrowser, Me.Response)


End Sub


~
ERROR RETURNED - Fails on line 48 (see below)

Server Error in ‘/TestAsynchClientWeb’ Application.

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Source Error:

Line 46: loStream = New MemoryStream(loArray)
Line 47: loStream.Seek(0, SeekOrigin.Begin)
Line 48: loDoc = loWord.Open(loStream)
Line 49:
Line 50: loDoc.Save(“Test”, SaveFormat.FormatDocument, SaveType.OpenInBrowser, Me.Response)


Source File: C:\Inetpub\wwwroot\TestAsynchClientWeb\ReportTest.aspx.vb Line: 48

Stack Trace:


[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index] System.Collections.ArrayList.get_Item(Int32 index) +91 Aspose.Word.File.d.a(aa A_0) Aspose.Word.File.ae.b(Int32 A_0) Aspose.Word.File.ak.a(Int32 A_0) Aspose.Word.File.ak.a(aa A_0) Aspose.Word.File.ak.a(Int32 A_0, Int32 A_1) Aspose.Word.File.ak.b() Aspose.Word.File.ae.a(by A_0) Aspose.Word.Document.a(by A_0) Aspose.Word.Document…ctor(Stream stream) Aspose.Word.Word.Open(Stream stream) TestAsynchClientWeb.ReportTest.Button1_ServerClick(Object sender, EventArgs e) in C:\Inetpub\wwwroot\TestAsynchClientWeb\ReportTest.aspx.vb:48 System.Web.UI.HtmlControls.HtmlInputButton.OnServerClick(EventArgs e) System.Web.UI.HtmlControls.HtmlInputButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) System.Web.UI.Page.ProcessRequestMain()

Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573
~


Roman - this is a major part of what we want to achieve with the product and I need to demo it before I move forward - any help or code examples in VB of similar use of the product in this manner would be greatly appreciated.


Hi Jeff,

I can assure you saving into a memory stream and later opening a document from an array of bytes works well. I have created a test to demonstrate just that:

///


/// A customer reported that saving and opening via memory stream does not work.
///

[Test]
public void TestOpenSaveByteArray()
{
TestUtil.SetUnlimitedLicense();
Document srcDoc = TestUtil.Open(@“Blank.doc”);

MemoryStream streamOut = new MemoryStream();
srcDoc.Save(streamOut, SaveFormat.FormatDocument);

byte[] docBytes = streamOut.ToArray();
MemoryStream streamIn = new MemoryStream(docBytes);

Word word = new Word();
Document dstDoc = word.Open(streamIn);

Assertion.AssertEquals("\r\n", dstDoc.Range.Text);
}

In your case you transport that byte array via web services. Apparently, the byte array does not get transferred properly. It is either mangled because of some incorrect encoding or just has not arrived fully because of some asynchronous methods.

I’m not very experienced in transporting and marshaling data over web services, but I’m pretty sure it has to do with this issue. You might be in a better position to resolve this.

As a simple test try to transport some array of bytes (size about the same as the document file) and make sure you get it transported with correct content and correct size.

Let me know if you cannot get it right, I will then schedule someone to help you with the webservices marshaling.