chiname

在IE中打开及下载文件的格式汇总

关于这方面的文章其实网上很多,有时候大家都会用到,在这里做一下汇总。
不全之处还请大家提出,多多留言阿......

一、在IE中直接打开指定的文件
   Response.Clear();
   Response.Buffer = true;
   Response.Charset = "utf-8";
   switch(type)
   {
    case "jpg":
     Response.ContentType = "image/JPEG";
     break;
    case "bmp":
     Response.ContentType = "image/BMP";
     break;
    case "xls":
     Response.ContentType = "application/vnd.ms-excel";
     break;
    case "pdf": 
     Response.ContentType = "application/pdf";
    break;
    case "word":
    Response.ContentType = application/msword";
    break;
    case "powerpoint":
    Response.ContentType = "application/vnd.ms-powerpoint";
    break;
    case "text":
    Response.ContentType = "text/plain";
    break;
    case "html":
    Response.ContentType = "text/html";
    break;
    default:
     Response.ContentType = "octet-stream";       
     Response.AddHeader("content-disposition","attachment; filename=" + HttpUtility.UrlEncode(FileName));    
     break; 
  }
   Response.OutputStream.Write(bytes,0,count);//其中bytes是文件的二进制流
   Response.End();

二、下载文件(其实与直接打开很类似啦)
    Response.Clear();
   Response.Buffer = true;
   Response.Charset = "utf-8"; 
   Response.ContentType = "application/octet-stream"; 
  Response.AddHeader("content-disposition","attachment; filename=" + HttpUtility.UrlEnco(FileName)); //其中filename要根据不同文件拼接不同后缀(通过switch判断,filename+=".xls")
  Response.OutputStream.Write(bytes,0,count);//其中bytes是文件的二进制流
   Response.End();

分类:

技术点:

相关文章:

  • 2022-01-27
  • 2021-11-20
  • 2022-02-24
  • 2022-02-03
  • 2021-12-02
猜你喜欢
  • 2021-06-02
  • 2022-12-23
  • 2021-12-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案