开发者

HttpContext.Current.Response write file after download manager starts

I'm trying to create a ZIP file on the fly which might contain a few thousands of pictures.

public static void CompressAndSendFiles(List files) { Http开发者_运维知识库Context.Current.Response.ClearContent();

    // LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header
    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"Jpeg Package " 
                                            + DateTime.Now.ToString("MM-dd-yyyy hh-mm-ss") + ".zip\"");

    // Add the file size into the response header
    //HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());

    // Set the ContentType
    HttpContext.Current.Response.ContentType = ReturnHttpContentType("download.zip");

    ZipOutputStream oZipStream = 
                   new ZipOutputStream(HttpContext.Current.Response.OutputStream);

    try
    {
        foreach (string file in files)
        {


            FileInfo fInfo = new FileInfo(file);
            ZipEntry oZipEntry = new ZipEntry(fInfo.Name);
            oZipStream.PutNextEntry(oZipEntry);
            byte[] buffer = File.ReadAllBytes(file);
            oZipStream.Write(buffer, 0, buffer.Length);

            //oZipStream.Flush();
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        oZipStream.Finish();
        oZipStream.Close();
        oZipStream.Dispose();

     }
     HttpContext.Current.Response.OutputStream.Flush();
     HttpContext.Current.Response.End();
}

Everything is fine, unless when number of files get big.

My question: Is there a way to initiate the download (let the download manager on client side popup), and then start writing on the stream?

I monitored w3wp.exe (IIS) process, and it seems that the data is being written on memory instead of Stream. When w3wp.exe memory usage riches a certain number, it releases the memory and nothing happens (no download).

Thank you in advance.


Have you tried using this?

HttpContext.Current.Response.BufferOutput = false;


As far as I know, you cannot stream it while archiving. You need to zip them first, then stream it. As the result, server is finally out of memory if the final zip is extremely large.

What I end up doing is I write it to a temporary zip file then stream that temporary file.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜