asp.net-mvc – FileContentResult和国际字符
发布时间:2020-12-16 07:07:45 所属栏目:asp.Net 来源:网络整理
导读:我正在使用fileContentResult将文件呈现给浏览器.除了在fileName包含国际字符时抛出异常,它才能正常工作. 我记得在某个地方读过这个功能不支持国际字符,但我确信在应用程序需要在美国以外的国家上传文件的情况下,必须有一个解决方法或最佳实践. 有谁知道这样
我正在使用fileContentResult将文件呈现给浏览器.除了在fileName包含国际字符时抛出异常,它才能正常工作.
我记得在某个地方读过这个功能不支持国际字符,但我确信在应用程序需要在美国以外的国家上传文件的情况下,必须有一个解决方法或最佳实践. 有谁知道这样的做法?这是ActionResult方法 public ActionResult GetFile(byte[] value,string fileName) { string fileExtension = Path.GetExtension(fileName); string contentType = GetContentType(fileExtension); //gets the content Type return File(value,contentType,fileName); } 提前致谢 苏珊 解决方法public class UnicodeFileContentResult : ActionResult { public UnicodeFileContentResult(byte[] fileContents,string contentType) { if (fileContents == null || string.IsNullOrEmpty(contentType)) { throw new ArgumentNullException(); } FileContents = fileContents; ContentType = contentType; } public override void ExecuteResult(ControllerContext context) { var encoding = UnicodeEncoding.UTF8; var request = context.HttpContext.Request; var response = context.HttpContext.Response; response.Clear(); response.AddHeader("Content-Disposition",string.Format("attachment; filename={0}",(request.Browser.Browser == "IE") ? HttpUtility.UrlEncode(FileDownloadName,encoding) : FileDownloadName)); response.ContentType = ContentType; response.Charset = encoding.WebName; response.HeaderEncoding = encoding; response.ContentEncoding = encoding; response.BinaryWrite(FileContents); response.End(); } public byte[] FileContents { get; private set; } public string ContentType { get; private set; } public string FileDownloadName { get; set; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 在Azure部署期间(彻底改变)Web.config
- asp.net-mvc – Bootstrap模式表单在提交后不会关闭
- Response.Redirect()vs Response.RedirectPermanent()
- Page_Load在ASP.NET页面中触发两次
- asp.net – EF在现实生活中,我想相信(有没有人有很好的指向
- asp.net – 使用Web.Config转换插入多个项目
- 如何在ASP.NET MVC网站中为cookie设置“安全”标志?
- asp.net-web-api – 刷新令牌 – 服务器端存储和撤销多个客
- 将自定义ValueProviderFactories添加到ASP.NET MVC3?
- ASP.NET如何在没有最终用户删除的情况下在屏幕上编写消息?
推荐文章
站长推荐
- 接受List的ASP.NET Web方法失败,“Web服务方法名
- asp.net-mvc – MVC3中的CheckboxList查看并获取
- 备忘录——IIS程序修改不生效Temporary ASP.NET
- Asp.net 5 MVC 6,添加facebook电子邮件的权限
- asp.net-core – 为什么默认的mvc6模板针对多个D
- asp.net – HTTP 500内部错误 – IIS网站
- FormsAuthenticationModule使用ASP.NET MVC验证事
- asp.net – mvc大写模型与小写模型
- asp.net-mvc – 通用列表属性的必需属性
- asp.net – 事件日志应用程序错误,故障地址有意义
热点阅读