我正在为Sharepoint编写一个应用程序,它在功能区上实现了一个按钮,可以将多个文件下载为zip ...
一切顺利,一切顺利......但当我尝试使用Chrome或Firefox下载zip时,他们什么都不做..
我的代码是:
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();
}
}
答案 0 :(得分:1)
删除Content-Length并在代码中使用Flush()然后使用End(),不要在代码末尾使用Close(),在声明所有内容之前可以使用它。当你不知道文件类型是什么时,通常会使用八位字节流,所以如果你知道文件类型是什么,请远离它。使用application / zip作为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();
答案 1 :(得分:0)
您是否尝试Application/octet-stream
作为MIME类型?
或
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();
}
}