分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

Asp.Net导出文件名中文乱码

发布时间:2023-09-06 02:27责任编辑:郭大石关键词:乱码

Asp.Net导出word为例,Excel等其他文件也一样

protected void Page_Load(object sender, EventArgs e) ???{string html = “网页html代码”; ???????string fileName = "故事.doc"; ???????DownloadDoc(fileName,html); ???} ???public void DownloadDoc(string fileName, string pageHtml) ???{ ???????//设置Http的头信息,编码格式 ?????????HttpContext.Current.Response.Buffer = true; ???????HttpContext.Current.Response.Clear(); ???????HttpContext.Current.Response.Charset = "gb2312"; ???????HttpContext.Current.Response.ClearContent(); ???????HttpContext.Current.Response.ClearHeaders(); ???????Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); ???????HttpContext.Current.Response.ContentType = "application/ms-word"; ???????HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + clFielName(fileName)); ???????//关闭控件的视图状态 ?,如果仍然为true,RenderControl将启用页的跟踪功能,存储与控件有关的跟踪信息 ???????this.EnableViewState = false; ???????HttpContext.Current.Response.Write(pageHtml); ???????HttpContext.Current.Response.End(); ???} ???public string clFielName(string fileName) ???{ ???????System.Web.HttpContext curContext = System.Web.HttpContext.Current; ???????string outputFileName = null; ???????string browser = curContext.Request.UserAgent.ToUpper(); ???????if (browser.Contains("MS") == true && browser.Contains("IE") == true) ???????{ ???????????outputFileName = System.Web.HttpUtility.UrlEncode(fileName); ???????} ???????else if (browser.Contains("FIREFOX") == true) ???????{ ???????????outputFileName = "\"" + fileName + "\""; ???????} ???????else ???????{ ???????????outputFileName = System.Web.HttpUtility.UrlEncode(fileName); ???????} ???????return outputFileName; ???}

判断不同的浏览器,然后编码

Asp.Net导出文件名中文乱码

原文地址:https://www.cnblogs.com/webapi/p/10148555.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved