1  System.Text.Encoding code = System.Text.Encoding.GetEncoding("UTF-8");
 2                 HttpContext.Current.Response.ContentEncoding = code;
 3                 HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.Default;
 4 
 5                 if (fileName.Contains(".xls"|| fileName.Contains(".xlsx"))
 6                 {
 7                     HttpContext.Current.Response.ContentType = "application/vnd.ms-excel;chartset=utf-8";
 8 
 9                 }
10 
11                 else
12                 {
13                     HttpContext.Current.Response.ContentType = "application/x-msdownload";
14                 }
15                 HttpContext.Current.Response.AppendHeader("Connection""keep-alive");
16 
17                
18 
19                 HttpContext.Current.Response.AppendHeader("Content-Disposition""attachment;filename=" + fileName);
20 
21                 HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
22 
23                 byte[] fileBuffer;
24                 
25                 if (fileSize > 0)
26                 {
27                     fileBuffer = new byte[fileSize];
28                     fileStream.Read(fileBuffer, 0, (int)fileSize);
29 
30                 }
31                 else
32                 {
33                     //文件流长度为空时,构造一个不为0的字节数组
34                     fileBuffer = new byte[1];
35                 }
36                 fileStream.Close();
37                 HttpContext.Current.Response.BinaryWrite(fileBuffer);
38                 
39                 HttpContext.Current.Response.Flush();
40                 HttpContext.Current.Response.End();

相关文章:

  • 2021-11-23
  • 2021-12-27
  • 2021-07-15
  • 2021-12-27
  • 2021-09-15
  • 2022-01-24
  • 2022-12-23
猜你喜欢
  • 2021-09-05
  • 2021-10-16
  • 2022-12-23
  • 2021-12-27
  • 2021-12-27
  • 2021-12-27
相关资源
相似解决方案