开发者

How can I send the client multiple files to download

开发者 https://www.devze.com 2023-03-03 01:16 出处:网络
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:

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

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)

I was trying to do something like this:

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.


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).


To send multiple file to be downloaded, you should zip them using sharpziplib or other file zipping utility,files should be zipped and then download link can be send to the client to download them at once. the code below use ICSharpCode.SharpZipLib.dll Library. You can call this class and pass your files which you want to zip.

public string Makezipfile(string[] files)
    {

  string[] filenames = new string[files.Length];


        for (int i = 0; i < files.Length; i++)
            filenames[i] = HttpContext.Current.Request.PhysicalApplicationPath + files[i].Replace(HttpContext.Current.Request.UrlReferrer.ToString(), "");
    string DirectoryName = filenames[0].Remove(filenames[0].LastIndexOf('/'));
    DirectoryName = DirectoryName.Substring(DirectoryName.LastIndexOf('/') + 1).Replace("\\", "");

    try
    {

        string newFile = HttpContext.Current.Request.PhysicalApplicationPath + "your image directory\\" + DirectoryName + ".zip";
        if (File.Exists(newFile))
            File.Delete(newFile);

        using (ZipFile zip = new ZipFile())
        {

            foreach (string file in filenames)
            {

                string newfileName = file.Replace("\\'", "'");
                zip.CompressionLevel = 0;
                zip.AddFile(newfileName, "");
            }

            zip.Save(newFile);
        }
    }
    catch (Exception ex)
    {
        //Console.WriteLine("Exception during processing {0}", ex);
        Response.Write(ex);
        // No need to rethrow the exception as for our purposes its handled.
    }
    string a;
    a = "your images/" + DirectoryName + ".zip";
    return a; 

}


I acknowledge the good Zip solutions mentioned here, but alternatively could you make 3 calls to the handler using javascript/XHR, requesting a different file format each time?

Admittedly, you are restricted by the number of concurrent requests supported by the browser, though I believe the browser will queue requests over the limit.

The benefit is that the User won't need to deal with a zip file, which may confuse them. Instead they should get 3 separate downloads.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号