【问题标题】:Sending multiple Aspose.words generated documents to client machine将多个 Aspose.words 生成的文档发送到客户端机器
【发布时间】:2014-04-29 21:34:58
【问题描述】:

我有一个 .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

【问题讨论】:

  • 这是一个相当普遍的问题。你的意思是自动通过电子邮件发送文件吗?上传到网站?
  • 对不起。我的意思是在生成文件时立即发送到客户端机器。提示用户理想地打开或保存生成的文件。

标签: asp.net vb.net aspose aspose.words


【解决方案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);

【讨论】:

  • 感谢 Nausherwan 的回复。我实际上能够使用您展示的方法将单个文件发送到客户端。我的问题是如何向客户端发送多个文件。我在我的问题中添加了一些额外的代码来说明我的挑战。
  • 好吧,在这种情况下,您需要先使用一些免费的库来压缩文件,然后再发送给客户端。如果您使用的是 .NET Framework 4.5,可以查看以下链接。 codeguru.com/csharp/.net/…。您也可以参考以下链接以使用第三方 dll 压缩文件。 codeproject.com/Tips/622788/…
【解决方案2】:

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

【讨论】:

  • 这是一个托管在远程服务器上的 Web 应用程序,所以我猜这个解决方案行不通,对吗?
  • 不,不会。抱歉,我错过了这是一个 ASP.NET 问题
【解决方案3】:

我找到了解决方案。我在 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    • 2012-04-03
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 2011-04-09
    • 1970-01-01
    相关资源
    最近更新 更多