質問

I have a .NET application that builds 4 documents in either .docx or .pdf format using the Aspose.words dlls. My challenge right now is how to deliver all 4 documents to the client right after generation. Has anyone done this before and, if so, how did you go about it? I'm able to send single files to the client but when I try to send multiple files the client only receives the last file specified in the code. For example:

Dim CLdoc As New Document("C:/Temp/Cover Letter.docx")
Dim CLbuilder As New DocumentBuilder(CLdoc)

'Build CLDoc content

Dim MTdoc As New Document("C:/Temp/Master Terms.docx")
Dim MTbuilder As New DocumentBuilder(MTdoc)

'Build MTDoc content

CLdoc.Save(Response, "iama.docx", ContentDisposition.Inline, Nothing)
MTdoc.Save(Response, "iama1.docx", ContentDisposition.Inline, Nothing)

The only document that gets sent to the client is 'iama1.docx'. How can I get the application to send both? One idea I had was to send both files to a zip archive and send that to the client but I really have no idea how to accomplish that. Any ideas?

EDIT

Using Ionic Zip I've tried saving the generated files to a memory stream, adding it to a zip archive and saving to disk (just temporarily for testing; I'll eventually send the zip archive to the client machine). My problem now is the .docx files I've added is blank/empty. Am I doing something wrong in how I'm saving the generated file to the memory stream?

Dim CLdoc As New Document("C:/Temp/Cover Letter.docx")
Dim CLbuilder As New DocumentBuilder(CLdoc)

'Build CLDoc content

Dim MTdoc As New Document("C:/Temp/Master Terms.docx")
Dim MTbuilder As New DocumentBuilder(MTdoc)

'Build MTDoc content    

Dim CLDocStream As New MemoryStream
Dim MTDocStream As New MemoryStream

CLdoc.Save(CLDocStream, SaveFormat.docx)
MTdoc.Save(MTDocStream, SaveFormat.docx)

Using zip1 As New ZipFile()

    zip1.AddEntry("CL.docx", CLDocStream)
    zip1.AddEntry("MT.docx", MTDocStream)
    zip1.Save("c:/Temp/test.zip")

End Using
役に立ちましたか?

解決 3

I found the solution. The reason I was getting empty files in my zip archive was that the position of the memory stream at the time I'm adding the entries to the zip archive is at the end of the stream. When I pointed the memory stream back to the beginning of the stream right before adding the entries it works like a charm. Revised code (including streaming the zip archive to the client):

Dim CLdoc As New Document("C:/Temp/Cover Letter.docx")
Dim CLbuilder As New DocumentBuilder(CLdoc)

'Build CLDoc content

Dim MTdoc As New Document("C:/Temp/Master Terms.docx")
Dim MTbuilder As New DocumentBuilder(MTdoc)

'Build MTDoc content    

Dim CLDocStream As New MemoryStream
Dim MTDocStream As New MemoryStream

CLdoc.Save(CLDocStream, SaveFormat.docx)
MTdoc.Save(MTDocStream, SaveFormat.docx)

CLDocStream.Seek(0, SeekOrigin.Begin)
MTDocStream.Seek(0, SeekOrigin.Begin)

Dim ZipStream As New MemoryStream()

Response.Clear()
Response.ContentType = "application/zip"
Response.AddHeader("Content-Disposition", "attachment;filename=Docs.zip")

Using zip1 As New ZipFile()

    zip1.AddEntry("CL.docx", CLDocStream)
    zip1.AddEntry("MT.docx", MTDocStream)
    zip1.Save(Response.OutputStream)

End Using

ZipStream.WriteTo(Response.OutputStream)
Response.End()

他のヒント

I work as Social Media Developer at Aspose. Check the following sample to send the documents to the client right after generation.

Document doc = new Document("input.doc");

//Do you document processing

//Send the generated document using Response Object.
doc.Save(Response, "output.doc", ContentDisposition.Inline, null);

If your program is running on the client's PC, then you can use Process.Start(filename) to load the document using the registered application (e.g. Word or Adobe Reader)

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top