I generate reports on my MVC application witch I then export to pdf and witch is then saved in my ~/Reports/Invoices/
Directory. I want to download these files one by one.
Here is my class:
public class DownloadResult : ActionResult
{
public DownloadResult()
{
}
public DownloadResult(string virtualPath)
{
this.VirtualPath = virtualPath;
}
public string VirtualPath
{
get;
set;
}
public string FileDownloadName
{
get;
set;
}
public override void ExecuteResult(ControllerContext context) {
if (!String.IsNullOrEmpty(FileDownloadName)) {
context.HttpContext.Response.AddHeader("content-disposition",
"attachment; filename=" + this.FileDownloadName);
}
string filePath = context.HttpContext.Server.MapPath(this.VirtualPath);
context.HttpContext.Response.TransmitFile(filePath);
}
}
and here is my controller action:
public ActionResult Download(int id)
{
return new DownloadResult
{
VirtualPath = "~/Reports/Invoices/" + Table.Where(x => x.ID == id).FirstOrDefault().ID + ".pdf",
FileDownloadName = Table.Where(x => x.ID == id).FirstOrDefault().ID.ToString()
};
}
When I try to use this code all it does is fill the partial view with symbols like when you try to open a binary file in notepad.
Any ideas on what I'm doing wrong?
I have Tried this:
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);
}
And get the same results
Invoices.GridLoadDone = function () {
$.contextMenu({
selector: '#gbox_invoiceGrid',
callback: function (key, options) {
var m = "clicked: " + key;
switch (key) {
case "view":
Globals.PerformAjaxFromHyperlink(null, '#/Invoice/View/' + Invoices.CurrentRow, true, Invoices.CurrentRow);
Globals.SetUrl('#/Invoice/View/' + Invoices.CurrentRow, false);
return true;
break;
case "email":
Globals.PerformAjaxFromHyperlink(null, '/Invoice/Email/' + Invoices.CurrentRow, false);
break;
case "download":
Globals.PerformAjaxFromHyperlink(null, '/Invoice/Download/' + Invoices.CurrentRow, false);
//$("#readingsGrid").jqGrid('editRow', Readings.CurrentRow,
break;
}
},
items: {
"view": {
name: "View Invoice"
},
"email": {
name: "Email Invoice"
},
"download": {
name: "Download Invoice"
},
}
});