我怎样才能发送客户端的多个文件下载 [英] How can I send the client multiple files to download

查看:111
本文介绍了我怎样才能发送客户端的多个文件下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个处理程序(ashx的)服务的一些文件。我有我的地方存放电子书的文件夹。我的本本PK他们的名字,并且每本书可以有几个不同的格式:

I'm using a handler(.ashx) to serve some files. I have a folder where I store ebooks. I name them by the books PK, and each book may have a few different formats:

211.html
211.pdf
211.prc

下面的测试code成功下载一本书。

The following test code successfully downloads one book.

context.Response.ContentType = "application/octet-stream";
context.Response.AppendHeader("Content-Disposition", "attachment;filename=myfile.pdf");
context.Response.TransmitFile(context.Server.MapPath("~/Media/eBooks/212.pdf"));

我怎么能服务于客户端的三种不同的格式? (该客户现有的组织不是一个文件夹中)

How can I serve the client the three different formats? (The clients existing organization isn't in a folder)

我试图做这样的事情:

DirectoryInfo bookDir  = new DirectoryInfo(context.Server.MapPath("~/Media/eBooks"));
FileInfo[] f  = bookDir.GetFiles();

foreach (var n in f)
{
    context.Response.AppendHeader("Content-Disposition", "attachment;filename=myfile.pdf");
    context.Response.TransmitFile(context.Server.MapPath("~/Media/eBooks/212.pdf"));
}

但它会下载一个文件,没有文件扩展名。

But it downloads one file with no file extension.

推荐答案

您可以在一个响应发送多个文件的唯一方法就是把它们存档包,里面例如一个 .zip文件文件。的的至少是一些可以用code来完成,使用各种工具(IIRC存在的主要.NET框架内的一个zip打包现在;否则,SharpZipLib会做的工作很好)。

The only way you can send multiple files in one response is to put them inside an archive package, e.g. a .zip file. That is at least something that can be done with code, using various tools (IIRC there's a zip packager inside the main .NET framework now; otherwise, SharpZipLib will do the job nicely).

这篇关于我怎样才能发送客户端的多个文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆