我有这个ajax电话:
var datiInput = {};
datiInput.idTipo = idtipo;
datiInput.clearEngine = clearengine;
datiInput.nomeFileSvg = numfile;
var jsonData = JSON.stringify(datiInput);
OpenLoading();
$.ajax({
type: "POST",
url: "handlers/generaDatiTecniciPDF.ashx",
data: jsonData,
contentType: "application/pdf",
dataType: "text",
success: function (data) {
CloseLoading();
//i wanna open pdf recived in new tab :(
}
});
这是我的处理程序ashx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Script.Serialization;
using iTextSharp.text;
using iTextSharp.text.pdf;
using ImageMagick;
namespace Federal_Mogul.handlers
{
/// <summary>
/// Descrizione di riepilogo per generaDatiTecniciPDF
/// </summary>
public class generaDatiTecniciPDF : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/pdf";
string datiJson = new StreamReader(context.Request.InputStream).ReadToEnd();
Input_DatiTecnici obj = Deserialize<Input_DatiTecnici>(datiJson);
if (obj != null)
{
int idtipo = Convert.ToInt32(obj.idTipo);
string ce = obj.clearEngine;
string svg = obj.nomeFileSvg;
string percorso_svg = @"http://www.website.it/svgs/" + svg + ".svg";
DatiVeicolo dV = GetDatiVeicolo(idtipo);
List<DatiTecnici> elencoDatiTecnici = new List<DatiTecnici>();
//elaboration of elencoDatiTecnici
CreaFilePDFDatiTecnici(dV, elencoDatiTecnici, idtipo, ce, percorso_svg, context);
}
}
public DatiVeicolo GetDatiVeicolo(int idtipo) //, int idbody)
{
// function for getting data of veichle
}
public void CreaFilePDFDatiTecnici(DatiVeicolo dV, List<DatiTecnici> dati, int idtipo, string clearengine, string svg, HttpContext context)
{
var document = new Document(PageSize.A4, 50, 50, 25, 25);
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
document.Open();
//elaboration of pdf
document.Close();
context.Response.BinaryWrite(output.ToArray());
}
public string GetNumeroSchedaDaBodyType(int idtipo, int idbody)
{
// function for obtain schedule number string
}
public class GradientTableBackground : IPdfPTableEvent
{
//class for managing gradient on table
}
public class DatiVeicolo
{
public string marca { get; set; }
public string modello { get; set; }
public string versione { get; set; }
public string cod_motore { get; set; }
}
public class DatiTecnici
{
public int Sort3 { get; set; }
public int idGruppo { get; set; }
public string Gruppo { get; set; }
public int IdSottogruppo { get; set; }
public string Sottogruppo { get; set; }
public string Dati { get; set; }
public string Valori { get; set; }
public string Discriminante { get; set; }
public string codMotore { get; set; }
public string ClearEngine { get; set; }
public int cTypeId { get; set; }
public int TypeID { get; set; }
}
public class Input_DatiTecnici
{
public int idTipo { get; set; }
public string clearEngine { get; set; }
public string nomeFileSvg { get; set; }
}
public T Deserialize<T>(string context)
{
string jsonData = context;
//cast to specified objectType
var obj = (T)new JavaScriptSerializer().Deserialize<T>(jsonData);
return obj;
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
如何响应pdf对象并在新标签中调用ajax后显示?没有关闭任何其他选项卡?
答案 0 :(得分:0)
你可以将它作为blob下载为它创建一个blob网址,然后用它打开一个新标签。
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if (this.readyState == 4 && this.status == 200){
var url = window.URL || window.webkitURL;
window.open(url.createObjectURL(this.response));
}
}
xhr.open('POST', 'handlers/generaDatiTecniciPDF.ashx');
xhr.responseType = 'blob';
xhr.setRequestHeader('Content-Type', 'application/pdf');
xhr.send(jsonData);