I want to deliver attachments from a third party system. They're stored as .attach
files in the filsystem, original files are present in his database. Following this approach, my action look like this:
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;
}
This works, pdf files are embedded in browser and binary files like zip got downloaded. But when viewing pdfs, the browser show me 1234.attach
as title in those example. I want to display the real file name (my-pdf.pdf
here) and found Content-Disposition
header for this.
Altough, Firefox and Chrome still display the id instead of my-pdf.pdf
. When inspecting the page, it shows those title tag:
<title>1234</title>
What am I doing wrong?
Avoid setting the filename as id
Found this question where it seems that the id
get parameter is used as filename. In my case its a real numeric id (1234) and I need to keep it since the files got verified by the database which use those id.