0

When I use the following code to download a ZIP file it appears to work. However, when I attempt to open the downloaded ZIP, I get an 'invalid compressed folder' message. When I open the ZIP in notepad I see it is filled with HTML.

string fp = Server.MapPath("directory\\file.zip");
FileInfo file = new FileInfo(fp);

if (file.Exists)
{
    Response.ClearContent();
    Response.AddHeader("content-disposition","attachment; filename=" + file.Name);
    Response.AddHeader("content-length", file.Length.ToString());
    Response.ContentType = "application/zip";
    Response.TransmitFile(file.FullName);
    Response.End();
}

An issue I can't seem to fix that is probably related is when I try to manually type in the address of the file (http://website.com/downloads/file.zip), I get a redirect (http://website.com/login.aspx) even when logged in as the admin. Any pointers in where to look would be greatly appreciated.

4

1 に答える 1

1

Response.ClearContent()also を使用する代わりにResponse.ClearHeaders()、現在のすべてのヘッダーと応答の本文を削除します。

MSDN から、HttpResponse.ClearContent メソッド:

ClearContent メソッドは、ヘッダー情報をクリアしません。

于 2012-05-26T07:49:03.637 に答える