Show or load a file correctly in ASP.NET MVC3 when the file type is not text or image

This is my action:

public FileResult ShowFile(long Id) {

    DAL.Files.File file = new DAL.Files.FileAccess().GetById(Id);
    return File(file.Content, file.FileType);
}

GetById the method returns a file from the SQL database when the file type is text or image, everything is fine and the file displayed correctly, but when the file type is different from the PDF, which shows some Unicode, since I can show the PDF file correctly or allow the user to download it ?

+3
source share
2 answers

The second argument to the method File()must be a mime type. So say your file is a PDF file, it should work if you do this:

public FileResult ShowFile(long Id) {

    DAL.Files.File file = new DAL.Files.FileAccess().GetById(Id);
    // assuming file is a PDF
    return File(file.Content, "application/pdf");
}

, File(), :

public FileResult ShowFile(long Id) {

    DAL.Files.File file = new DAL.Files.FileAccess().GetById(Id);
    // assuming file is a PDF
    return File(file.Content, "application/pdf", "some-pdf-file.pdf");
}
0

, /pdf, , .

    public FileResult ShowFile(long Id)
    {

        DAL.Files.File file = new DAL.Files.FileAccess().GetById(Id);
        // assuming file is a PDF
        Response.AppendHeader("Content-Disposition", "inline; filename=CustomInvoice.pdf");
        return File(file.Content, "application/pdf", "some-pdf-file.pdf");
    }
0

All Articles