0

Currently I'm working on an app where a dialog pops up with details of an item being received (the user just inputs the amount received). The user inputs the changes (amount received) and then saves. Once saved, I prompt the user to create a pdf via confirmation (the client wanted to see changes in a hard copy). The pdf is correctly generated and offered to the user via an Open/Save/Cancel dialog.

My question is, is there a way to just force the pdf to open automatically without the prompt to Open/Save/Cancel? The reason why is I want to be able to shut the dialog behind the scenes as the pdf pops up so the user doesn't have to close it themselves.

Below is what I have so far.

View

$.getJSON('@Url.Action("ReceiveProduct", "PurchaseOrder")', model, function (result) { //Save was successful if (result) { var createPrintOut = confirm("Do you wish to create a print out?"); if (createPrintOut) { var url = '@Url.Action("ViewPrintOut", "PurchaseOrder")'; window.location = url + "?POId=" + $("#PurchaseOrderId").val() + "&PId=" + $("#ProductId").val(); } 

Controller

public ActionResult ViewPrintOut(int POId, int PId) { //Make sure user has proper permissions if (!_permissionService.Authorize(StandardPermissionProvider.ManageOrders)) return AccessDeniedView(); var purchaseOrder = _purchaseOrderService.GetPurchaseOrderById(POId); string fileName = string.Format("purchaseorderproduct_{0}_{1}.pdf", PId, DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")); string filePath = System.IO.Path.Combine(this.Request.PhysicalApplicationPath, "content\\files\\ExportImport", fileName); _pdfService.PrintPurchaseOrderProductsToPdf(purchaseOrder, PId, _workContext.WorkingLanguage, filePath); var bytes = System.IO.File.ReadAllBytes(filePath); return File(bytes, "application/pdf", fileName); } 

1 Answer 1

1

Add the following line before the return statement.

Response.AppendHeader("Content-Disposition", "inline; filename="+ filename); 

and remove the filename reference in the Return.

return File(bytes, "application/pdf"); 

Change the Window.Location to

window.open(Yoururl,'_blank'); 
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for that answer. One other question. How can I make it appear within another tab instead? I don't want to lose the page the user is currently on.
Add target="_blank" to the link you use to trigger the operation(i mean in the Print Pdf confirmation), can you?
I'm setting window.location at the moment. This pdf is auto generated and not accessed via a link. That's been part of my issue with it mostly.
You are overwriting the current page by changing the window.location. See my edit.
Using window.open I forced a new window to open, however, I have to force the page to refresh before I get my pdf. Hmm, may need to keep chipping away at it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.