I am trying to call my FileStream Result function from an ajax, the question is that it is calling the method and it does everything inside, I checked this with a breakpoint, but at the end it is not downloading the pdf I want. Before it was downloading and it was calling it from
like this: “using (Html.BeginForm("EmisionAllLabel", "WareHouseReceipt", FormMethod.Post))
“. But now I need to do it from an ajax but it doesn’t work for me. Maybe it’s because I’m missing something but I don’t have much experience with ajax regarding this and I can’t identify the problem. I appreciate your comments.
Attached are the codes used:
$('#btnValidarEmision').on("click", function () {
let emisionguia = $('#guiaAerea').val();
$.ajax({
url: "/WareHouseReceipt/" + "EmisionEtiqueta",
type: "POST",
data: { "guiaAerea": emisionguia },
success: function (saved) {
},
error: function () {
console.log("Error");
}
});
});
FileStreamResult
public FileStreamResult EmisionEtiqueta(string guiaAerea)
{
IList<ICriterion> criterionlist = new List<ICriterion>();
criterionlist.Add(Expression.Eq("Econtainer", true));
criterionlist.Add(Expression.Eq("StatusId", 1));
criterionlist.Add(Expression.Eq("Emision", false));
IList<VwWareHouseReceipt> etiquetas = vwWarehouseReceiptDao.findByCriteria(criterionlist);
//mem buffer
MemoryStream ms = new MemoryStream();
//the document
Document document = new Document(new Rectangle(float.Parse("289.50"), 370, 0, 0));
BaseFont bf2 = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
BaseFont helveticanormal = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, true);
BaseFont helveticanegrita = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, true);
//create the fonts
BaseFont timesNormal = BaseFont.CreateFont(BaseFont.TIMES_ROMAN,
BaseFont.CP1252,
BaseFont.NOT_EMBEDDED);
Font fontSmall = new Font(timesNormal, 7, Font.NORMAL);
Font fontNormal = new Font(timesNormal, 9, Font.NORMAL);
Font fontH1 = new Font(timesNormal, 16, Font.NORMAL);
//the writer
PdfWriter writer = PdfWriter.GetInstance(document, ms);//fs);
// step 3: we open the document
document.Open();
for (int i = 0; i < etiquetas.Count; i++) {
}
document.Close();
//close the document
// document.PageSize = PageSize.HALFLETTER;
//document.PageSize = PageSize.
//prepare output stream
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=EtiquetasEmisionEcont" + guiaAerea + ".pdf");
Response.Buffer = true;
Response.Clear();
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
Response.OutputStream.Flush();
Response.End();
return new FileStreamResult(Response.OutputStream, "application/pdf");
Note: I deleted the “for” so that there would not be too much text.