问题源于FilePathResult内置了文件编码方式,该方式与utf-8不同导致文件名出现乱码。要解决这个问题,可以自定义一个重载的File方法,并设置编码为utf-8。
以下是示例代码:
public ActionResult File(string fileName, string contentType, string path) { byte[] fileBytes = System.IO.File.ReadAllBytes(path); string fullFileName = Path.GetFileName(path);
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=\"" + fullFileName +"\"");
Response.ContentType = contentType;
Response.ContentEncoding = Encoding.UTF8; // 设置编码为UTF-8
return File(fileBytes, contentType);
}