110,545
社区成员
发帖
与我相关
我的任务
分享
System.Web.UI.Page page = new System.Web.UI.Page();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.ContentEncoding = Encoding.Default;
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + page.Server.UrlEncode(fileInfo.Name));
//HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name);
HttpContext.Current.Response.WriteFile(fileInfo.FullName);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
$.ajax({
url: "../Ashx/ExportExcel.ashx?excelType=user",
cache: false,
error: function(msg) {
$.messager.alert("提示", "系统发生异常,请联系相关人员。");
return;
}
});
应该没问题吧。。。
//以字符流的形式下载文件
FileStream fs = new FileStream(fileInfo.FullName, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
HttpContext.Current.Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileInfo.Name, System.Text.Encoding.UTF8));
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();