2016-05-06 61 views
0

我有這樣的Ajax調用:顯示PDF

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

或者我必須創建pdf文件併發送鏈接需求?謝謝 –

回答

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);  
+0

感謝@Musa我如何傳遞參數 –

+0

'jsonData'是你在請求中發送的json,所以將你的數據傳遞給'xhr.send()'函數 – Musa

+0

哦,愚蠢的抱歉:)。我現在就試一試。很快就會見到你,我接受它的答案! –