I have the following ActionResult:
[Authorize]
public ActionResult DownloadFile(Guid id)
{
AdjusterFile file = (from f in db.AdjusterFiles
join a in db.Adjusters
on f.adjusterID equals a.id
join u in db.Users
on a.userID equals u.id
where u.username == HttpContext.User.Identity.Name
&& f.id == id
select f)
.FirstOrDefault();
return new FileContentResult(file.fileContent.ToArray(), file.fileContentType);
}
It works perfectly for word documents (DOC/DOCX). However, whenever I try to download PDFs which are uploaded by the same process, it says, "Failed to load PDF document" in Chrome. In IE, it says, "The file is damaged and could not be repaired."
The content type is "application/pdf."
How can I download PDFs in MVC?
Relevant file upload code:
using (MemoryStream ms = new MemoryStream())
{
file.InputStream.CopyTo(ms);
byte[] array = ms.GetBuffer();
AdjusterFile newFile = new AdjusterFile();
newFile.id = Guid.NewGuid();
newFile.adjusterID = adj.id;
newFile.type = "eo";
newFile.fileName = file.FileName;
newFile.fileContent = array;
newFile.fileContentType = file.ContentType;
db.AdjusterFiles.InsertOnSubmit(newFile);
}
return new FileContentResult()tryreturn File()(I don't remember in what version of MVC that was added, but it's definitely in 4). Barring that, there's no difference in the file result functionality between any file types. Are you sure the PDF itself isn't damaged in some way? Maybe some character encoding issue when it was stored in or retrieved from the database?return File(...but it still doesn't work (I get the same error). I have edited to question to include the code I am using to upload the file. Perhaps you can determine whether or not there is something I am doing wrong for PDF documents.