ITPub博客

首页 > IT职业 > IT图书 > MIME让TXT可下载

MIME让TXT可下载

IT图书 作者:cylong 时间:2021-09-09 09:41:39 0 删除 编辑

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

 

图片描述

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();
        }
图片描述

 


 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2005/viewspace-2811440/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-16

  • 博文量
    106
  • 访问量
    1359