Hi mahesh213,
You can't directly return a file for download via an HTTP service call.
So an alternative approach is generate the file as byte array and save in the TempData and return JsonResult to with FileName to success function and redirects to separate controller action with filename and download using TempData in browser.
Check this example. Now please take its reference and correct your code.
Namespaces
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
return View();
}
public JsonResult getAll()
{
return Json(GetEmployees(), JsonRequestBehavior.AllowGet);
}
[HttpPost]
public ActionResult GenerateReport(int eId)
{
string html = "";
Employee employee = GetEmployees().Where(x => x.EId == eId).FirstOrDefault();
// Company Logo and Name.
html += "<table>" +
"<tr><td colspan='2'><img src='" + GetUrl("Files/Logo.png") + "' height='50px' width='100px' /></td></tr>" +
"<tr><td>Company : </td><td><b>Excelasoft Solutions</b></td></tr>" +
"<tr><td colspan='2'> </td></tr></table>";
// Employee Data.
html += "<table border='1' cellpadding='0' cellspacing='0' width='100%'><tr>" +
"<th>Id</th>" +
"<th>Name</th>" +
"<th>Gender</th>" +
"<th>Phone No</th>" +
"<th>Location</th>" +
"<th>Company</th>" +
"<th>Id</th>" +
"<th>Name</th>" +
"<th>Gender</th>" +
"<th>Phone No</th>" +
"<th>Location</th>" +
"<th>Company</th>" +
"<th>Id</th>" +
"<th>Name</th>" +
"<th>Gender</th>" +
"<th>Phone No</th>" +
"<th>Location</th>" +
"<th>Company</th>" +
"<th>Id</th>" +
"<th>Name</th>" +
"<th>Gender</th>" +
"<th>Phone No</th>" +
"<th>Location</th>" +
"<th>Company</th>" +
"<th>Id</th>" +
"<th>Name</th>" +
"<th>Gender</th>" +
"<th>Phone No</th>" +
"<th>Location</th>" +
"<th>Company</th>" +
"</tr><tr>" +
"<td>" + employee.EId + "</td>" +
"<td>" + employee.EName + "</td>" +
"<td>" + employee.Gender + "</td>" +
"<td>" + employee.PNo + "</td>" +
"<td>" + employee.Location + "</td>" +
"<td>" + employee.Company + "</td>" +
"<td>" + employee.EId + "</td>" +
"<td>" + employee.EName + "</td>" +
"<td>" + employee.Gender + "</td>" +
"<td>" + employee.PNo + "</td>" +
"<td>" + employee.Location + "</td>" +
"<td>" + employee.Company + "</td>" +
"<td>" + employee.EId + "</td>" +
"<td>" + employee.EName + "</td>" +
"<td>" + employee.Gender + "</td>" +
"<td>" + employee.PNo + "</td>" +
"<td>" + employee.Location + "</td>" +
"<td>" + employee.Company + "</td>" +
"<td>" + employee.EId + "</td>" +
"<td>" + employee.EName + "</td>" +
"<td>" + employee.Gender + "</td>" +
"<td>" + employee.PNo + "</td>" +
"<td>" + employee.Location + "</td>" +
"<td>" + employee.Company + "</td>" +
"<td>" + employee.EId + "</td>" +
"<td>" + employee.EName + "</td>" +
"<td>" + employee.Gender + "</td>" +
"<td>" + employee.PNo + "</td>" +
"<td>" + employee.Location + "</td>" +
"<td>" + employee.Company + "</td>" +
"</tr></table>";
using (MemoryStream stream = new MemoryStream())
{
StringReader sr = new StringReader(html);
Document pdfDoc = new Document(PageSize.B2, 5f, 5f, 10f, 5f);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream);
pdfDoc.Open();
XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr);
pdfDoc.Close();
TempData["Data"] = stream.ToArray();
}
return new JsonResult() { Data = new { FileName = employee.EName.Trim().Replace(" ", "_") + ".pdf" } };
}
[HttpGet]
public virtual ActionResult Download(string fileName)
{
if (TempData["Data"] != null)
{
byte[] data = TempData["Data"] as byte[];
return File(data, "application/pdf", fileName);
}
else
{
return new EmptyResult();
}
}
private string GetUrl(string imagePath)
{
string appUrl = System.Web.HttpRuntime.AppDomainAppVirtualPath;
if (appUrl != "/")
{
appUrl = "/" + appUrl;
}
string url = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, appUrl);
return url + imagePath;
}
private List<Employee> GetEmployees()
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee { EId = 1, EName = "Maria", Gender = "Female", PNo = "030-0074321", Location = "Austria", Company = "Alfreds Futterkiste" });
employees.Add(new Employee { EId = 2, EName = "Antonio Moreno", Gender = "Male", PNo = "(5) 555-3932", Location = "Brazil", Company = "Antonio Moreno Taquería" });
employees.Add(new Employee { EId = 3, EName = "Thomas Hardy ", Gender = "Male", PNo = "(5) 555-3932", Location = "Ireland", Company = "Around the Horn" });
return employees;
}
public class Employee
{
public int EId { get; set; }
public string EName { get; set; }
public string Gender { get; set; }
public string PNo { get; set; }
public string Location { get; set; }
public string Company { get; set; }
}
}
View
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Index</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.js"></script>
<script type="text/javascript">
var app = angular.module('MyApp', []);
app.controller('MyController', ['$scope', '$http', function ($scope, $http) {
$http({
method: 'GET',
url: '/Home/getAll/'
}).success(function (data) {
$scope.items = data;
});
$scope.GenerateReport = function (id) {
$http({
method: 'POST',
url: '/Home/GenerateReport/',
params: { eId: id }
}).success(function (data) {
window.location = '/Home/Download?filename=' + data.FileName;
});;
};
} ]);
</script>
</head>
<body ng-app="MyApp" ng-controller="MyController">
<div class="container" id="printarea">
<table class="table table-bordered">
<tr class="success">
<th>EId</th>
<th>EName</th>
<th>Gender</th>
<th>PNo</th>
<th>Location</th>
<th>Company</th>
<th>Ation</th>
</tr>
<tr ng-repeat="item in items">
<td>{{item.EId}}</td>
<td>{{item.EName}}</td>
<td>{{item.Gender}}</td>
<td>{{item.PNo}}</td>
<td>{{item.Location}}</td>
<td>{{item.Company}}</td>
<td>
<button class="btn btn-success btn-sm" ng-click="GenerateReport(item.EId)">
<span class="glyphicon glyphicon-print"></span> PDF
</button>
</td>
</tr>
</table>
</div>
</body>
</html>
Screenshot
