简体   繁体   English

ZIP下载包含html内容

[英]ZIP download contains html content

When I use the following code to download a ZIP file it appears to work. 当我使用以下代码下载ZIP文件时,它似乎可以工作。 However, when I attempt to open the downloaded ZIP, I get an 'invalid compressed folder' message. 但是,当我尝试打开下载的ZIP时,收到“无效的压缩文件夹”消息。 When I open the ZIP in notepad I see it is filled with HTML. 当我在记事本中打开ZIP时,我看到它充满了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. 我似乎无法解决的一个可能与之相关的问题是,当我尝试手动键入文件的地址( http://website.com/downloads/file.zip )时,出现了重定向( http://website.com/login.aspx ),即使以管理员身份登录也是如此。 Any pointers in where to look would be greatly appreciated. 任何指针在哪里看将不胜感激。

Instead of just using Response.ClearContent() also use Response.ClearHeaders() to remove all the current headers as well as the body of the response. 除了使用Response.ClearContent()还可以使用Response.ClearHeaders()删除所有当前标头以及响应的正文。

From MSDN, HttpResponse.ClearContent Method : 来自MSDN的HttpResponse.ClearContent方法

The ClearContent method does not clear header information. ClearContent方法不会清除标头信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM