0

我有一个 .NET 应用程序,它使用 Aspose.words dll 以 .docx 或 .pdf 格式构建 4 个文档。我现在面临的挑战是如何在生成后立即将所有 4 个文档交付给客户。以前有没有人这样做过,如果有,你是怎么做的?我可以向客户端发送单个文件,但是当我尝试发送多个文件时,客户端只收到代码中指定的最后一个文件。例如:

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)

发送给客户端的唯一文档是“iama1.docx”。我怎样才能让应用程序同时发送?我的一个想法是将两个文件都发送到一个 zip 存档并将其发送给客户端,但我真的不知道如何实现这一点。有任何想法吗?

编辑

使用 Ionic Zip 我尝试将生成的文件保存到内存流中,将其添加到 zip 存档并保存到磁盘(只是暂时用于测试;我最终会将 zip 存档发送到客户端计算机)。我现在的问题是我添加的 .docx 文件是空白/空的。我在如何将生成的文件保存到内存流中做错了什么?

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
4

3 回答 3

1

我在 Aspose 担任社交媒体开发人员。检查以下示例以在生成后立即将文档发送给客户端。

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);
于 2014-04-30T05:38:43.007 回答
0

如果您的程序在客户端的 PC 上运行,那么您可以使用Process.Start(filename)已注册的应用程序(例如 Word 或 Adob​​e Reader)加载文档

于 2014-04-30T03:36:06.140 回答
0

我找到了解决方案。我在 zip 存档中获得空文件的原因是,当我将条目添加到 zip 存档时,内存流的位置位于流的末尾。当我在添加条目之前将内存流指向流的开头时,它就像一个魅力。修改后的代码(包括将 zip 存档流式传输到客户端):

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()
于 2014-04-30T21:02:19.900 回答