Ziba Leah
Ziba Leah

Reputation: 2514

Zip Content Type in a Response with Firefox And Chrome

I'm writing an application for Sharepoint that implements a button on a ribbon to download multiple files as a zip...

Everything goes fine, everything goes well... But when I tried to download the zip with with Chrome or Firefox they do nothing..

My code is this:

private void WriteStreamToResponse(MemoryStream ms)
    {
        if (ms.Length > 0)
        {
            string filename = DateTime.Now.ToFileTime().ToString() + ".zip";
            Response.Clear();
            Response.ClearHeaders();
            Response.ClearContent();
            Response.ContentType = "application/zip"; //also tried application/octect and application/x-zip-compressed
            Response.AddHeader("Content-Length", ms.Length.ToString());
            Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);

            byte[] buffer = new byte[65536];
            ms.Position = 0;
            int num;
            do
            {
                num = ms.Read(buffer, 0, buffer.Length);
                Response.OutputStream.Write(buffer, 0, num);
            }

            while (num > 0);

            Response.Flush();
        }
    }

Upvotes: 0

Views: 4161

Answers (2)

TGarrett
TGarrett

Reputation: 552

Remove the Content-Length and also have Flush() and then End() in your code, do not use Close() at the end of your code, you can use that before you declare everything. Octet-stream is generally used when you do not know what the file type will be, so stay away from that if you know what the file type will be. Use application/zip as the Content-Disposition.

       string filename = DateTime.Now.ToFileTime().ToString() + ".zip";
        Response.Clear();
        Response.BufferOutput = false;
        Response.ClearHeaders();
        Response.ClearContent();
        Response.ContentType = "application/x-zip-compressed"; //also tried application/octect and application/x-zip-compressed         
        Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);

        byte[] buffer = new byte[65536];
        ms.Position = 0;
        int num;
        do
        {
            num = ms.Read(buffer, 0, buffer.Length);
            Response.OutputStream.Write(buffer, 0, num);
        }

        while (num > 0);

        Response.Flush();
        Response.End();

Upvotes: 1

kolin
kolin

Reputation: 2344

have you tried Application/octet-stream as the MIME-type?

or

private void WriteStreamToResponse(MemoryStream ms)
{
    if (ms.Length > 0)
    {
        byte[] byteArray = ms.ToArray();
        ms.Flush();
        ms.Close();

        string filename = DateTime.Now.ToFileTime().ToString() + ".zip";
        Response.BufferOutput = true;
        Response.Clear();
        Response.ClearHeaders();
        Response.ClearContent();
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Length", ms.Length.ToString());
        Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);

        Response.BinaryWrite(byteArray);
        Response.End();

    }
}

Upvotes: 0

Related Questions