Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys,

I use this code for a download function but when I run the code it displays what looks like a binary file that is opened in a text editor.

C#
public FileResult Download(int id)
        {
            byte[] fileBytes = System.IO.File.ReadAllBytes(Server.MapPath("~/Reports/Invoices/" + Table.Where(x => x.ID == id).FirstOrDefault().ID + ".pdf"));
            string fileName = Table.Where(x => x.ID == id).FirstOrDefault().ID.ToString();
            return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Pdf, fileName);
        }
Posted

Hi
Can you try with 'FileContentResult()' I.E. return new FileContentResult(....)



Regards
Dominic
 
Share this answer
 
Comments
Member 10554372 6-Feb-14 2:29am    
The reason this is not working is because I use ajax for the download link witch I am told will not work. I have no choice but to use ajax so is there any other way in witch I can do a file download
you must set the content type before returning the result to the user
return File(byte[], "application/zip", path + ".zip");
 
Share this answer
 
you must set the content type before returning the result to the user. Also, it is better to put the header by hand,
C#
not System.Net.Mime.MediaTypeNames.Application.Pdf

C#
return File(byte[], "application/zip", path + ".zip");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900