【发布时间】:2018-12-11 11:17:23
【问题描述】:
我想从第三方系统传送附件。它们在文件系统中存储为.attach 文件,原始文件存在于他的数据库中。跟随this approach,我的动作是这样的:
public async Task<IActionResult> GetAttachment(int id) {
var attachment = await myAttachmentManager.GetAttachmentAsync(id);
// attachmentsPath = T:\attachments, FilePath = 1234.attach
string fullPath = Path.Combine(attachmentsPath, attachment.FilePath);
var content = await File.ReadAllBytesAsync(fullPath);
var fileContentProvider = new FileExtensionContentTypeProvider();
string mimeType;
if (!fileContentProvider.TryGetContentType(attachment.FileName, out mimeType)) // FileName = my-file.pdf
mimeType = "application/octet-stream";
var file = new FileContentResult(content, mimeType);
// This should display "my-file.pdf" as title
var contentHeader = new ContentDispositionHeaderValue("inline") {
FileName = attachment.FileName
}.ToString();
Response.Headers["Content-Disposition"] = contentHeader;
return file;
}
这可行,pdf 文件嵌入在浏览器中,并且下载了 zip 等二进制文件。但是在查看 pdf 时,浏览器会在这些示例中显示 1234.attach 作为标题。我想显示真实的文件名(这里是my-pdf.pdf)并找到了Content-Disposition这个标题。
尽管如此,Firefox 和 Chrome 仍然显示 id 而不是 my-pdf.pdf。检查页面时,它会显示这些标题标签:
<title>1234</title>
我做错了什么?
避免将文件名设置为id
找到this question,似乎id get 参数被用作文件名。在我的情况下,它是一个真实的数字 id (1234),我需要保留它,因为文件已由使用这些 id 的数据库验证。
【问题讨论】:
标签: c# http asp.net-core download asp.net-core-2.2