2016-05-06 2 views
0

Ich habe diesen Anruf Ajax:anzeigen pdf auf dem Server mit Ajax-Aufruf erzeugt

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 :(
    } 
}); 

Dies ist mein Handler 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; 
      } 
     } 
    } 
} 

Wie kann Antwort pdf Objekt und zeigt nach Aufruf Ajax in einem neuen Tab ? Ohne Close irgendwelche anderen Tabs?

+0

Oder ich muss PDF-Datei erstellen und Link Bedürfnisse senden? Danke –

Antwort

0

Sie könnten es als Blob herunterladen erstellen Sie eine Blob-URL für sie und öffnen Sie dann einen neuen Tab mit ihm.

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

danke @Musa wie kann ich Parameter übergeben –

+0

'jsonData' ist der JSON, den Sie in der Anfrage senden, so übergeben Sie Ihre Daten an die' xhr.send() '-Funktion – Musa

+0

oh dumm sorry :). Ich werde es jetzt versuchen. Bis bald und ich akzeptiere die Antwort, wenn es funktioniert! –

Verwandte Themen