做了一个.zip文件下载,下载完成后,删除文件。但是我发现不同的浏览器执行不同(360急速浏览器的急速模式下正常)。有的浏览器,下载代码会执行两次,当执行第一次时把文件删除了,执行第二次就找不到文件。我网上找了好多,但是找不到。有谁遇到过并且解决了。赐教下呗。
这是代码:
#region 下载数据
public void Downloadfile(int id)
{
string filename = "ExportData" + id + ".zip";
string path = Server.MapPath("XML") + "//" + filename;
try
{
a = 0;
if (File.Exists(path))
{
FileInfo fi = new FileInfo(path);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(path), System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", fi.Length.ToString());
Response.TransmitFile(fi.FullName);
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
db.deletefile(Server.MapPath("XML") + "//", filename);//删除函数
}
else
{
Response.Write("<script langauge=javascript>alert('文件不存在,请先导出、压缩后再下载!');location.href='Index.aspx';</script>");
}
}
catch (Exception ex)
{
//db.labMessage = ex.Message;
}
}
#endregion
response.end(); 处理完要结束
加这个会报错,我网上找了答案用HttpContext.Current.ApplicationInstance.CompleteRequest();代替response.end();