• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
老司机快发车
记录自己遇到的一些问题,以便后期查看
博客园    首页    新随笔    联系   管理    订阅  订阅

C# 下载文本文件乱码

if (File.Exists(filePath))
{
    FileInfo fi = new FileInfo(filePath);
    context.Response.Clear();
    context.Response.ClearHeaders();
    context.Response.Buffer = true;
    context.Response.Charset = "GB2312";
    //判断是否是IE浏览器
    if (explorerName.Contains("IE"))
        //是IE就对字符串编码
        context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpContext.Current.Server.UrlEncode(Path.GetFileName(filePath)));
    else
        //不是IE就不编码(Firefo、Chrome编码后会显示乱码)
        context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + Path.GetFileName(filePath));
    context.Response.AppendHeader("Content-Length", model.VideoSize.ToString());
    context.Response.ContentType = "application/octet-stream";
    context.Response.Filter.Close();
    context.Response.TransmitFile(filePath);
    context.Response.Flush();
    context.Response.End();
}
posted @ 2021-05-06 14:50  老司机快发车  阅读(449)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3