Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Jquery AJAX Request, Controller Response "send_data" , PDF as Ajax Response unable to download.

jquery rails

  • Please log in to reply
1 reply to this topic

#1 RAJESH

RAJESH

    Passenger

  • Members
  • 1 posts

Posted 05 September 2013 - 10:40 AM

Hi,

 

Could anyone please help. how can i prompt the download!

 

Ajax call :

 

$.ajax({
      type: "GET",
      url: pdfDownloadUrl,
      cache: false,
      beforeSend: function(){
          console.log("sending ajax call ...");
       },
      success:function(result){

          console.log(result);
          console.log("Ajax call success ...");
      }
});

 

 

In Controller:

 

send_data sample_pdf, :filename => "filename.pdf", :type => "application/pdf", :disposition => "attachment"

 

 

Response:

 

is like

 

%PDF-1.4 1 0 obj << /Title () /Creator () /Producer (Qt 4.8.4 \(C\) 2011 Nokia Corporation and/or its subsidiary\(-ies\)) /CreationDate (D:20130905160726+05'30') >> endobj 3 0 obj << /Type /ExtGState%PDF-1.4 1 0 obj << /Title () /Creator () /Producer (Qt 4.8.4 \(C\) 2011 Nokia Corporation and/or its subsidiary\(-ies\)) /CreationDate (D:20130905160726+05'30') >> endobj 3 0 obj << /Type /ExtGState /SA true /SM 0.02 /ca 1.0 /CA 1.0 /AIS false /SMask /None>> endobj 4 0 obj [/Pattern /DeviceRGB] endobj 6 0 obj << /Type /XObject /Subtype /Image /Width 533 /Height 98 /BitsPerComponent 8 /ColorSpace /DeviceGray /Length 7 0 R /Filter /FlateDecode >> stream xһQBA@ 0y8_"vs*}pߏ lmvs_Xg )S LA0a )S LA0a )S LA0a )S LA0a )S LA0a )S LA0a )S LA0a )S LA0a )S LA0a )S LA0a )S LA8,4g'mf'm<]^+_.Bendstream endobj 7 0 obj 294 endobj 8 0 obj << /Type /XObject /Subtype /Image /Width 533 /Height 98 /BitsPerComponent 8 /ColorSpace /DeviceRGB /SMask 6 0 R /Length 9 0 R /Filter /FlateDecode >> stream x=AEQ<? ԍ<XMseW4Mao6_Z~/i|\F>vz">ic#'9N z~>W>,gx<4|6=x\F>FD2n85$+endstream endobj 9 0 obj 504 endobj 10 0 obj << /Type /XObject /Subtype /Image /Width 532 /Height 13 /BitsPerComponent 8 /ColorSpace /DeviceGray /Length 11 0 R /Filter /FlateDecode >> stream xء0DQ=#P@d@2UŪu1ZRQUִv l& ]tvi >_>gv <~(endstream endobj 11 0 obj 107 endobj 12 0 obj << /Type /XObject /Subtype /Image /Width 532 /Height 13 /BitsPerComponent 8 /ColorSpace /DeviceRGB /SMask 10 0 R /Length 13 0 R /Filter /FlateDecode >> stream xۻJ@Qҙ!3HDHc!1y eyc|_~zQUUYM<_R5mv2}E$@Buu]@axlW;VȖѶ;d!||@Ԏa۶eYy</8qu/pWEQO7&W_endstream endobj 13 0 obj 178 endobj 14 0 obj << /Type /XObject /Subtype /Image /Width 530 /Height 13 /BitsPerComponent 8 /ColorSpace /DeviceGray /Length 15 0 R /Filter /FlateDecode >> stream x! @@ѩ6El1Ȝ`&w~:T9*x' I6❀P2GX#`eٽ,wB%rXA[endstream endobj 15 0 obj 104 endobj 16 0 obj << /Type /XObject /Subtype /Image /Width 530 /Height 13 /BitsPerComponent 8 /ColorSpace /DeviceRGB /SMask 14 0 R /Length 17 0 R /Filter /FlateDecode >> stream x1J@k`02H Bû <Ts~/7>Zq]ץaqF7QhjԈ<@KTcghI)j,r@KT# -Pm-QR;D5r2MS|^ ;ϳR}J).]B0nZendstream endobj 17 0 obj 171 endobj 18 0 obj << /Type /XObject /Subtype /Image /Width 264 /Height 141 /BitsPerComponent 8 /ColorSpace /DeviceGray /Length 19 0 R /Filter /FlateDecode >> stream xб @*q6 BlL=osuyNg~C|!>ćC|!>ćC|!>ćC|!>ćC|!>ćC|!>ćC|!>ćC|!>ćC|!>ćC|!>ćC|!>ćC|!>ćC|!>ćC|!>ćC|!>ćC|!>ćC|!:`߹V'l5-\88?endstream endobj 19 0 obj 290 endobj 20 0 obj << /Type /XObject /Subtype /Image /Width 264 /Height 141 /BitsPerComponent 8 /ColorSpace /DeviceRGB /SMask 18 0 R /Length 21 0 R /Filter /FlateDecode >> stream xձjBQEA 4klrAdWLq,



#2 jack

jack

    Signalman

  • Moderators
  • 19 posts
  • LocationPoole

Posted 05 September 2013 - 11:35 AM

Hi Rajesh - Perhaps I'm misunderstanding, but this sort of call shouldn't render PDF data through an AJAX request. You'd be best off doing a browser redirect to the URL of the file, rather than pulling it through AJAX. This would trigger a download prompt by the browser.

 

Or am I missing the point?


  • Jamie likes this




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users