代码:
/// <summary>
/// 下载导入模板
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ExportBiaoZhun_Click(object sender, EventArgs e)
{
string strFilePath = Server.MapPath("~") + "/ZSK/问题上传模板.xls";//服务器文件路径
FileInfo fileInfo = new FileInfo(strFilePath);
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
string agent = "";
//string filename = "";
agent = Request.UserAgent;
//if (agent != null && agent != "" && agent.IndexOf("Edge") < 0)
//{
// filename = fileInfo.Name;
//}
//else
//{
// filename = Uri.EscapeDataString(fileInfo.Name);
//}
string name = fileInfo.Name;
if (Request.UserAgent.ToLower().IndexOf("msie") > -1) //ie浏览器
{
name = HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8);
}
if (Request.UserAgent.ToLower().IndexOf("firefox") > -1) //火狐浏览器
{
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + name + "\"");
}
else //其他浏览器,如谷歌浏览器
{
Response.AddHeader("Content-Disposition", "attachment;filename=" + name);
}
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.ContentType = "application/x-bittorrent";
Response.Charset = "utf-8";
Response.WriteFile(fileInfo.FullName);
Response.End();
}
在本地和测试站都ok,发布到正式站就成这样了,大神们是时候展现你们真正技术了!!!
试试 Response.ContentType = "application/vnd.ms-excel";