我的下载代码如下:
Response.Clear();
//清除缓冲区流中的所有头
Response.ClearHeaders();
//设置缓冲输出为false
Response.Buffer = false;
//设置输出流的 HTTP MIME 类型为application/octet-stream
Response.ContentType = "application/octet-stream;charset=gbk";
Response.AddHeader("Cache-Control", "no-cache");
//将 HTTP 头添加到输出流
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(path, System.Text.Encoding.UTF8));
Response.WriteFile(filePath);
Response.End();
在IE8和360中点击下载是直接就让用户保存然后才能打开,但是在IE6下是有打开,保存和取消3个功能,点击保存和IE8中情况是一样的,但是点击打开,提示“找不到文件C:\Documents and Settings\Administrator\Local Settings\Temporary Internet Files\Content.IE5\D225XQW\work[1].txt,要创建文件吗?” 选择是的时候只是在IE的临时文件夹里面创建了此文件,文件中无内容,请问是什么问题呢?
Response.Clear();
//清除缓冲区流中的所有头
Response.ClearHeaders();
//设置缓冲输出为false
Response.Buffer = false;
//设置输出流的 HTTP MIME 类型为application/octet-stream
Response.ContentType = "application/octet-stream;charset=gbk";
Response.AddHeader("Cache-Control", "no-cache");
//将 HTTP 头添加到输出流
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(path, System.Text.Encoding.UTF8));
Response.WriteFile(filePath);
Response.End();
在IE8和360中点击下载是直接就让用户保存然后才能打开,但是在IE6下是有打开,保存和取消3个功能,点击保存和IE8中情况是一样的,但是点击打开,提示“找不到文件C:\Documents and Settings\Administrator\Local Settings\Temporary Internet Files\Content.IE5\D225XQW\work[1].txt,要创建文件吗?” 选择是的时候只是在IE的临时文件夹里面创建了此文件,文件中无内容,请问是什么问题呢?
1.先取得文件字节长度.在输出时指定长度
Response.WriteFile(filePath, 0, fileSize);
2.加下两句
Response.Flush();
Response.Close();
string filePath = Server.MapPath("");//路径 FileInfo fileInfo = new FileInfo(filePath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();
Response.End();