I am working on an MVC3 application that needs to generate extensive reports regularly. Users can select their criteria and request the report, which currently opens in a new tab/window using the Javascript window.open() method. However, while the report is being generated, the user is unable to interact with the site as everything waits for the report to finish processing. The code snippet responsible for generating the report is as follows:
private FileStreamResult doSpecReport(List<int> idProjItems)
{
PdfDocument outputDocument = new PdfDocument(); // to be returned 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 the report to the 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'm uncertain about any mistakes in my approach, and I find it puzzling why this process consumes all of the browser's resources. Any insights or assistance would be greatly appreciated.
Update: Here is one version of the code calling the doSpecReport function. The success section appears to be malfunctioning.
$.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);
}
});