I have a FileStreamResult action that, with iTextSharp, opens a PDF document (CAD drawing), stamps our company information on it and then passes it to a memory stream to be returned.
Inside the method I have an audit method that records the transaction to SQL through Entity Framework. When I call the action I get three database transactions. While debugging the application hits the SQL record function three times.
Can anyone help me understand why? Does this have to do with routing?
EDIT I did some further testing and found that when I directly access the browser it only calls the method once. I am using jQuery with .PDFObject (www.pdfobject.com) to render the PDF inside another view. This jQuery method is calling the MVC method three times. Im still investigating...
public class DrawingsController : BaseController
{
static BaseFont ...
public FileStreamResult Index(string WOBase, string WOSub)
{
//Get associated drawing by work order
WorkOrders WO = db.WorkOrders
.Where(wo => wo.DRAWING_FILE != null && wo.BASE_ID == WOBase && wo.SUB_ID == WOSub)
.FirstOrDefault();
if (!string.IsNullOrEmpty(WO.DRAWING_FILE))
{
//The following records the transaction history (x3)
BaseController.RecordNavigation(_employee.ID, "Print", WO.Drawing_URL);
string readerURL = modifyPathToURL(WO.DRAWING_FILE);
Response.Clear();
MemoryStream ms = new MemoryStream();
PdfReader reader = new PdfReader(readerURL);
PdfStamper stamper = new PdfStamper(reader, ms);
for (int i = 1; i <= reader.NumberOfPages; ++i)
{
var stamperOverContent = stamper.GetOverContent(i);
//Stamp information on document
...
stamperOverContent.EndText();
stamperOverContent.RestoreState();
}
stamper.Writer.CloseStream = false;
stamper.Close();
byte[] byteinfo = ms.ToArray();
ms.Write(byteinfo, 0, byteinfo.Length);
ms.Position = 0;
ms.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(ms, "application/pdf");
}
return null;
}