c# - 从控制器发送文件以查看
问题描述
我正在尝试将文件从 MVC 5 控制器发送到 angularjs 客户端。我需要在响应中指定文件名。
[HttpGet]
public ActionResult DownloadAttachment([FromUri] long NoteId)
{
var attachment = db.tcp_tahGeneral_downloadNotesAttachment_API(NoteId).First();
var path = attachment.AttachmentLocation;
if (File.Exists(path))
{
FileStream stream = new FileStream(path, FileMode.Open);
byte[] file = new byte[stream.Length];
stream.Read(file, 0, file.Length);
return new FileContentResult(file, attachment.AttachmentFileType);
}
return null;
}
这会发送文件,但不允许我指定新文件名。
解决方案
FileContentResult中有一个FileDownloadName
设置文件名的属性,你可以试试下面的 return 语句。
return new FileContentResult(file, attachment.AttachmentFileType)
{
FileDownloadName = "myfilename.myext"
};
推荐阅读
- javascript - Prefix a string to json key in javascript
- eclipse - 如何使用 eclipse 运行一个简单的 openMP 示例?
- python - 如何在递归函数中再次要求用户输入
- javascript - Orbeon JavaScript 嵌入:a.querySelector 不是函数
- angular - 防止多次执行观察者
- java - 获得基于度数的正确随机线坐标的解决方案
- bash - 我正在使用 `xprintidle` 在 X 秒不活动后触发 bash 脚本。我如何让它循环?
- objectbox - ObjectBox DB 是否跨语言和平台兼容?
- ruby-on-rails - 如何在 Rails6 中的视图和控制器之外使用持久值
- java - 如何将任何对象的列表转换为具有相同类型的数组?