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

网站三站合一什么是网络营销含义

网站三站合一,什么是网络营销含义,网络客服在家上班招聘,移动网站建设自助建站原文地址为: 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.hkea.cn/news/315633/

相关文章:

  • 中国网站开发网站seo需要用到哪些工具
  • 织梦做的网站首页出现空白网页平台做个业务推广
  • 备案做电影网站吗yandx引擎入口
  • 网站双倍浮动百度账号登陆入口
  • 聊城市网站建设网站推广排名
  • 帝国新闻网站模板百度seo推广怎么做
  • 预约做港澳证的网站网站排名在线优化工具
  • 罗湖实惠的网站建设费用成都官网seo厂家
  • 建设部官方网站有哪些优帮云排名优化
  • 天津做网站找谁新东方在线教育平台官网
  • 南宁做网站在哪了日本预测比分
  • 咋样查看网站用什么编程语言做的9个广州seo推广神技
  • 网站链接太多怎么做网站地图谷歌广告
  • 网站关键词更新临汾网络推广
  • 个人做网站靠什么盈利免费网站建设模板
  • 网站开发 打标签aso优化怎么做
  • 教育校园网站建设方案seo每天一贴
  • 怎么看网站的建设时间推广公司品牌
  • 营销型网站有什么特点英语培训机构
  • 学院网站的系统建设方式宝鸡网站seo
  • 网站客户端怎么做的百度一下了你就知道官网
  • 有什么推广方法优化大师电脑版官方
  • 自己做网站的服务器上海网站建设公司排名
  • 跳蛋塞逼做多的视频网站百度广告联盟官网
  • 房地产网站开发文档企业查询
  • 做emu对网站有什么要求十大免费无代码开发软件
  • 扬州专业做网站做关键词优化
  • 宿州网站建设贰聚思诚信网站服务器
  • 用照片做模板下载网站好百度爱采购官方网站
  • 微网站建设套餐网络营销是做什么的