MIME让TXT可下载

简介:

这是以前写论坛的下载时用的代码 

 

复制代码
public   void  ProcessRequest(HttpContext context)
        {
            
string  name  =   " d:\\abc.txt " ;
            
// System.IO.FileInfo aFile = new System.IO.FileInfo(name);
            
// string na = Path.GetFileName(name); 
            
// context.Response.Clear();
            
// context.Response.ClearHeaders();
            
// context.Response.BufferOutput = false;   
           
//  context.Response.ContentType = "application/octet-stream";
            context.Response.AppendHeader( " Content-disposition " " attachment;filename=abc.txt " );
           
//  context.Response.AppendHeader("Content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(na, System.Text.Encoding.UTF8)); 
           
//  context.Response.AddHeader("Content-Length",aFile.Length.ToString());
            context.Response.WriteFile(name);
            
// context.Response.Flush();
            
// context.Response.End();
        }

        
public   bool  IsReusable
        {
            
get
            {
                
return   false ;
            }
        }


 
private   void  OutPutFile( string  filePath)
        {
            FileStream fs 
=  File.OpenRead(Server.MapPath(filePath));
            BinaryReader br 
=   new  BinaryReader(fs);
            Byte[] fileData 
=   new   byte [fs.Length];
            br.Read(fileData, 
0 , fileData.Length);
            Response.Clear();
            Response.ClearHeaders();
            Response.BufferOutput 
=   false ;  
            Response.ContentType 
=   " application/force-download " ;
            Response.AddHeader(
" Content-Disposition: " " attachment;filename= "   +  HttpUtility.UrlEncode(Path.GetFileName(filePath),System.Text.Encoding.UTF8));
            Response.AddHeader(
" Content-Length " , fileData.Length.ToString());
            Response.BinaryWrite(fileData);
            Response.Flush();
            br.Close();
            fs.Close();
            Response.End();
        }
复制代码

 

版权声明:本文原创发表于博客园,作者为路过秋天,原文链接:

http://www.cnblogs.com/cyq1162/archive/2010/04/18/896534.html

相关文章
|
4月前
|
Java
Java将指定文件/文件夹压缩成zip、rar压缩文件--解決中文乱码
Java将指定文件/文件夹压缩成zip、rar压缩文件--解決中文乱码
|
弹性计算 应用服务中间件
请将文件MP_verify_N8no8dpNO9n7wuBZ.txt上传至XXX指向的web服务器(或虚拟主机)的目录
请将文件MP_verify_N8no8dpNO9n7wuBZ.txt上传至XXX指向的web服务器(或虚拟主机)的目录
请将文件MP_verify_N8no8dpNO9n7wuBZ.txt上传至XXX指向的web服务器(或虚拟主机)的目录
|
Java 数据安全/隐私保护 Apache
|
JavaScript 前端开发 Java
Java http Post下载Excel文件 xxx.xlsx 失败解决,及传输文件类型ContentType对应关系
查询数据库生成Excel下载功能。本来很简单的功能却搞了挺久的,主要因为下载的文件打不开,且此功能已是基本功能。这里记录下来方便后面查阅。 ContentType对应的文件关系查看下面连接 https://www.runoob.com/http/mime-types.html
294 0
robots.txt文件示例
robots.txt文件示例
97 0
根据url下载文件为zip格式文件到客户端并解决火狐浏览器中文乱码问题
根据文件的远程路径将多文件合并成zip压缩包,然后下载到客户端,并解决火狐浏览器中文文件名乱码问题,下面是代码。
305 0
|
PHP
【PHP】上传文件提示No such file or directory
【PHP】上传文件提示No such file or directory
147 0
|
缓存 搜索推荐 前端开发
Robots.txt协议文件你了解多少,最详细的Robots.txt协议解读
Robots.txt协议文件你了解多少,最详细的Robots.txt协议解读在这,很多站长即使知道robots协议是干什么,但对于其深入的机制并没有了解,因此这里有必要说明一下。
Robots.txt协议文件你了解多少,最详细的Robots.txt协议解读