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

成都网站运营如何开网店做微商

成都网站运营,如何开网店做微商,济南官网seo推广,青岛做网站的好公司原文地址为: C#实现文件下载 1,Http 协议中有专门的指令来告知浏览器, 本次响应的是一个需要下载的文件. 格式如下:Content-Disposition: attachment;filenamefilename.ext以上指令即标记此次响应流是附件,且附件文件名为 filename.ext注意:…
原文地址为: C#实现文件下载

1,Http 协议中有专门的指令来告知浏览器, 本次响应的是一个需要下载的文件. 格式如下:
Content-Disposition: attachment;filename=filename.ext
以上指令即标记此次响应流是附件,且附件文件名为 filename.ext
注意:
(1): 中文文件名需要进行URLEncode编码, 否则在IE 6 下会提示是”无法识别的文件”.

但经实际测试,在Chrome下不进行URLEncode编码, 也能正常显示.

(2): 文件名不能有空格, 否则也会被认为是”无法识别的文件”.

(3): [ASP.Net中] 向响应流中添加该指令必须使用 response.AddHeader() 函数; 使用

response.Header.Add() 则会报错.

下面是一个实现下载文件功能的函数:

       /// <summary>/// 使用微软的TransmitFile下载文件/// </summary>/// <param name="filePath">服务器相对路径</param>public 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;filename=filename.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 name="filePath">相对路径</param>public 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 name="filePath"></param>public 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.yayakq.cn/news/677690/

相关文章:

  • 网站留言程序怎么做济南章丘网站建设
  • 哈尔滨口碑好的建站公司开锁在百度上做网站要钱吗
  • 小浪底水利枢纽建设管理局网站wordpress 标签输出页
  • 旅游网站建设策划方案书中山专业门户网站制作平台
  • 中国建设银行春招网站wap手机网站静态模板
  • 网络营销网站建设实验总结德州网站建设推广
  • 网站开发网上宠物店管理系统学网页设计在哪学
  • 设备租赁网站建设出名的建站网站
  • 常德做网站专业公司设计和建设一个网站要多少钱
  • 网站开发微博宁波城乡住房建设局网站
  • 新乡网站开发建站素材网站模板
  • 六安市裕安区建设局网站wordpress图片批量链接
  • 一级a做爰电影免费观看网站外贸网站租用外国服务器好还是自己装一个服务器好
  • 什么是三合一网站建设北京附近做网站的公司
  • 盘锦网站建设公司php网站后台怎么登陆
  • 深圳黑马品牌网站设计建筑人才网 中级职称评审费用
  • 淮南网站建设公司韩国最新新闻事件
  • 400电话单页网站3d效果图用什么软件
  • 门户网站建设成都现在去成都需要隔离吗
  • 网站建设和网页制作抖音代运营文员是干嘛的
  • 安阳网站设计哪家专业大良营销网站建设特色
  • 阿里云做的网站怎么样网站开发流程及进度安排
  • 余姚做网站设计制作网页软件教程
  • 有没有男女做那个的网站wordpress login插件
  • 网站建设属于什么经营范围动漫设计在哪里可以学
  • 营销型网站建设的价格中英 网站模板 带手机版
  • 哪些做图片赚钱的网站搭建什么网站好玩
  • 东莞做网站的公司哪家最好设计公司注册需要什么条件
  • 购物网站建设名字创建免费网站注意事项
  • 邯郸企业做网站费用做网站需要多少费用