gpt4 book ai didi

asp.net-mvc - FileContentResult 和国际字符

转载 作者:行者123 更新时间:2023-12-02 03:39:07 25 4
gpt4 key购买 nike

我正在使用 fileContentResult 将文件呈现到浏览器。它工作得很好,只是当文件名包含国际字符时它会抛出异常。我记得在某处读到过此功能不支持国际字符,但我确信在应用程序需要在美国以外的国家/地区上传文件时,人们必须遵循一种解决方法或最佳实践。

有人知道这样的做法吗?这是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-mvc - FileContentResult 和国际字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1139324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com