服务器启用了gizp,下载的excel模版会出现问题,怎么解决?
服务器相关参数:
IIS6.0
.net frmework3.5
启用IIS gzip压缩网站,压缩的参数里面没有包含xls后缀名
没有启用gizp时下载是正常了,启用了gizp就出现表头不见了,网上查找了下http://www.cnblogs.com/ppchen/archive/2009/02/19/1382530.html 里面是使用HttpModule
只有使用HttpModule这种方法吗?
下载文件代码
/// <summary>
/// 下载指定路径文件
/// </summary>
/// <param name="path">文件绝对路径</param>
public static void DownLoadFile(string path, string oldFileName)
{
System.IO.FileInfo fi = new System.IO.FileInfo(path);
if (fi.Exists)
{
//判断文件是否正在使用
try
{
using (System.IO.FileStream fs = System.IO.File.Open(path, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.None))
{
}
}
catch (Exception)
{
throw;
}
if (string.IsNullOrEmpty(oldFileName))
oldFileName = path.Substring(path.LastIndexOf("\\") + 1);
string browser = System.Web.HttpContext.Current.Request.UserAgent.ToUpper();
if (browser.Contains("FIREFOX") == true)
oldFileName = "\"" + oldFileName + "\"";
else if (browser.Contains("MSIE"))
oldFileName = ToHexString(oldFileName);
else
oldFileName = HttpUtility.UrlEncode(oldFileName);
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + oldFileName);
System.Web.HttpContext.Current.Response.AddHeader("Content-Length", fi.Length.ToString());
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream;charset=gb2321";
System.Web.HttpContext.Current.Response.WriteFile(fi.FullName);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.Close();
}
else
{
throw new Exception("文件在服务器上不存在!");
}
}
[解决办法]
public static void ResponseApplicationFile(this HttpContext context, string filePath, string saveAsName, bool isDirectOpen = false)
{
HttpResponse Response = context.Response;
Response.Clear();
Response.Buffer = true;
Response.ContentType = MIMEUtility.GetMIMEType(filePath);
string downloadFileSaveAsName = string.Empty;
if (string.IsNullOrWhiteSpace(System.IO.Path.GetFileNameWithoutExtension(saveAsName))
[解决办法]
string.IsNullOrWhiteSpace(saveAsName))
{
downloadFileSaveAsName = System.IO.Path.GetFileName(filePath);
}
else if (System.IO.Path.GetExtension(filePath).ToUpper() == System.IO.Path.GetExtension(saveAsName))
{
downloadFileSaveAsName = saveAsName;
}
else
{
downloadFileSaveAsName = saveAsName + System.IO.Path.GetExtension(filePath);
}
Response.HeaderEncoding = Encoding.UTF8;
if (System.IO.File.Exists(filePath))
{
Response.AddHeader("content-disposition", (isDirectOpen ? "" : "attachment;") + " filename=" + HttpUtility.UrlEncode(downloadFileSaveAsName, Encoding.UTF8) + ";size=" + (new FileInfo(filePath)).Length.ToString());
byte[] contents = System.IO.File.ReadAllBytes(filePath);
Response.BinaryWrite(contents);
}
else
{
Response.AddHeader("content-disposition", (isDirectOpen ? "" : "attachment;") + "filename=" + HttpUtility.UrlEncode(downloadFileSaveAsName, Encoding.UTF8) + ";size=0");
}
Response.Flush();
Response.End();
}
然后这样调用:
protected void Page_Load(object sender, EventArgs e)
{
this.Button_DownloadTemplateFile.Click += new EventHandler(Button_DownloadTemplateFile_Click);
}
void Button_DownloadTemplateFile_Click(object sender, EventArgs e)
{
string filename = "xxxxxx.xls";
string DownloadFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory+EnvironmentSetting.ThankyouLetterPath, filename);
if (!System.IO.File.Exists(DownloadFilePath))
{
this.MessageBoxLayer.Show("找不到相案");
return;
}
byte[] contents = System.IO.File.ReadAllBytes(DownloadFilePath);
FileInfo info = new FileInfo(DownloadFilePath);
long fileSize = info.Length;
this.Context.ResponseApplicationFile(DownloadFilePath, filename);
}
[解决办法]
同意楼上!
[解决办法]
晕!
那你写成 System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream;charset=gb2321" 这个头是要干什么啊?