I have an MVC3 application that needs to generate large reports on a regular basis. The user can choose their criteria and launch the report. Right now I am opening a new tab/window with the javascript window.open() method. While the report is getting generated the user can not use the site. Everything waits till the report is generated. The code for generating the report is:
private FileStreamResult doSpecReport(List<int> idProjItems)
{
PdfDocument outputDocument = new PdfDocument(); // returning to the user
foreach(var id in idProjItems)
{
var item = _entities.ProjectEquipmentItems.First(f => f.idProjectEquipmentItem == id);
var cutsheetPath = item.CutSheet;
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("p_idEquipmentItem", id.ToString());
var fs = GetReportHtml("NameOfReport", dictionary); // Returns FileStreamResult from crystal
var inputDocument1 = CompatiblePdfReader.Open(fs.FileStream); // add report to output doc
int count = inputDocument1.PageCount;
for(int idx = 0; idx < count; idx++)
{
PdfPage page = inputDocument1.Pages[idx];
outputDocument.AddPage(page);
}
if (!string.IsNullOrEmpty(cutsheetPath))
{
cutsheetPath = Path.Combine(Server.MapPath("~/Files/CutSheetFiles/"), cutsheetPath);
if (File.Exists(cutsheetPath))
{
var inputDocument2 = CompatiblePdfReader.Open(cutsheetPath);//, PdfDocumentOpenMode.Import);
count = inputDocument2.PageCount;
for(int idx = 0; idx < count; idx++)
{
PdfPage page = inputDocument2.Pages[idx];
outputDocument.AddPage(page);
}
}
}
}
var ms = new MemoryStream();
outputDocument.Save(ms, false);
ms.Position = 0;
return new FileStreamResult(ms, "application/pdf")
{
FileDownloadName = "Report.pdf"
};
}
I am not sure if I am doing anything wrong, I don't understand why this process takes up all the browser's resources. Thanks for any help.
Update: One version of the code that calls doSpecReport. The code around the success doesn't work.
$.ajax({
url: url,
data: qdata,
type: "POST",
success: function (result) { // this doesn't actually work.
var obj = $('<object type="application/pdf" width="100%" height="100%" border="2"></object>');
obj.attr('data', 'data:application/pdf;base64,' + result);
$(".mask").hide();
$('#divContainer').append(obj);
}
});