当前位置: 首页 > news >正文

企业网站的优化建议中陕核建设集团网站

企业网站的优化建议,中陕核建设集团网站,如何做一个自己的网站呢,荣成城市规划建设局网站原文地址为#xff1a; C#实现文件下载 1#xff0c;Http 协议中有专门的指令来告知浏览器, 本次响应的是一个需要下载的文件. 格式如下:Content-Disposition: attachment;filenamefilename.ext以上指令即标记此次响应流是附件,且附件文件名为 filename.ext注意#xff1a;…原文地址为 C#实现文件下载 1Http 协议中有专门的指令来告知浏览器, 本次响应的是一个需要下载的文件. 格式如下:Content-Disposition: attachment;filenamefilename.ext以上指令即标记此次响应流是附件,且附件文件名为 filename.ext注意(1): 中文文件名需要进行URLEncode编码, 否则在IE 6 下会提示是”无法识别的文件”.但经实际测试,在Chrome下不进行URLEncode编码, 也能正常显示.(2): 文件名不能有空格, 否则也会被认为是”无法识别的文件”.(3): [ASP.Net中] 向响应流中添加该指令必须使用 response.AddHeader() 函数; 使用response.Header.Add() 则会报错.下面是一个实现下载文件功能的函数 /// summary/// 使用微软的TransmitFile下载文件/// /summary/// param namefilePath服务器相对路径/parampublic void TransmitFile(string filePath){try{filePath Server.MapPath(filePath);if (File.Exists(filePath)){FileInfo info new FileInfo(filePath);long fileSize info.Length;HttpContext.Current.Response.Clear(); //指定Http Mime格式为压缩包HttpContext.Current.Response.ContentType application/x-zip-compressed;// Http 协议中有专门的指令来告知浏览器, 本次响应的是一个需要下载的文件. 格式如下:// Content-Disposition: attachment;filenamefilename.txtHttpContext.Current.Response.AddHeader(Content-Disposition, attachment;filename Server.UrlEncode(info.FullName));//不指明Content-Length用Flush的话不会显示下载进度 HttpContext.Current.Response.AddHeader(Content-Length, fileSize.ToString());HttpContext.Current.Response.TransmitFile(filePath, 0, fileSize);HttpContext.Current.Response.Flush();}}catch{ }finally{HttpContext.Current.Response.Close();}} 2 下面是使用WriteFile实现下载 /// summary/// 使用WriteFile下载文件 /// /summary/// param namefilePath相对路径/parampublic void WriteFile(string filePath){try{filePath Server.MapPath(filePath);if (File.Exists(filePath)){FileInfo info new FileInfo(filePath);long fileSize info.Length;HttpContext.Current.Response.Clear();HttpContext.Current.Response.ContentType application/octet-stream;HttpContext.Current.Response.AddHeader(Content-Disposition, attachement;filename Server.UrlEncode(info.FullName));//指定文件大小 HttpContext.Current.Response.AddHeader(Content-Length, fileSize.ToString());HttpContext.Current.Response.WriteFile(filePath, 0, fileSize);HttpContext.Current.Response.Flush();}}catch{ }finally{HttpContext.Current.Response.Close();}} 3下面是分块实现下载 /// summary/// 使用OutputStream.Write分块下载文件 /// /summary/// param namefilePath/parampublic void WriteFileBlock(string filePath){filePath Server.MapPath(filePath);if (!File.Exists(filePath)){return;}FileInfo info new FileInfo(filePath);//指定块大小 long chunkSize 4096;//建立一个4K的缓冲区 byte[] buffer new byte[chunkSize];//剩余的字节数 long dataToRead 0;FileStream stream null;try{//打开文件 stream new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);dataToRead stream.Length;//添加Http头 HttpContext.Current.Response.ContentType application/octet-stream;HttpContext.Current.Response.AddHeader(Content-Disposition, attachement;filename Server.UrlEncode(info.FullName));HttpContext.Current.Response.AddHeader(Content-Length, dataToRead.ToString());while (dataToRead 0){if (HttpContext.Current.Response.IsClientConnected){int length stream.Read(buffer, 0, Convert.ToInt32(chunkSize));HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);HttpContext.Current.Response.Flush();HttpContext.Current.Response.Clear();dataToRead - length;}else{//防止client失去连接 dataToRead -1;}}}catch (Exception ex){HttpContext.Current.Response.Write(Error: ex.Message);}finally{if (stream ! null){stream.Close();}HttpContext.Current.Response.Close();}} 转载请注明本文地址 C#实现文件下载
http://www.hkea.cn/news/14402494/

相关文章:

  • 公司百度网站建设朔州做网站
  • 厦门旅游集团网站建设网站浏览器兼容性通用
  • 怎么给一个网站做推广杭州室内设计工作室
  • 无锡滨湖住房与城乡建设局网站wordpress文件执行顺序
  • 免备案网站空间科技项目的类型有
  • 无法跳转到建设银行网站百度写作助手
  • 网站文章标题网站建设做的快
  • 电梯网站建设二维码网站建设
  • 伊宁市建设局网站网站建站公司哪家好
  • app设计网站模板免费下载汽车大全官网
  • 家装网站建设公司哪家好做私房蛋糕在哪些网站写东西
  • 手机网站建设推广软文canva可画官网
  • 卡密网站怎么做自己建设的网站如何优化
  • 网站托管服务 优帮云自家企业网络推广
  • 法律推广网站建设银行企业网银网站无法打开
  • 怎样做移动端网站wordpress 后台 添加菜单
  • 郑州网站建设首选创新app开发去哪个网站
  • 网站栏目页模板安全联盟可信任网站认证 网站
  • 用什么软件搭建网站源码做网站只开发手机端可不可以
  • 招生网站建设策划方案哪家公司做网站
  • 莱芜哪家企业做网站网站建设说课获奖视频
  • 谁会制作网站广告设计公司合同
  • 济南营销网站制作广告公司招聘
  • 贵阳经开区建设管理局网站当地的网站建设
  • 昆山网站建设培训内蒙古呼和浩特市邮编
  • 儿童做的小游戏下载网站软件开发代码大全
  • 南海区住房和城乡建设部网站好看简单易做的网站
  • asp.net 手机网站开发建网页的公司
  • 关掉wordpress站点竞价网站同时做优化可以吗
  • 网站备案的主体变更怎么做网站积分系统