1

i am using codeigniter and ajax for downloading a file.

My Ajax Request

    $('.exporter').bind('click',function()
    {
    var callfunction = 'reportprints/print_'+$(this).attr('id').toLowerCase();
    var Query = $('#exportQuery').val();                        
    $.ajax({
    url: callfunction,
    type: 'POST',           
    dataType: 'html',
    data:{q:Query},                                                         
    success: function(output)
    {}
    });
    });

it's calling my PDF Function which are in Controller

    public function print_pdf()
    {
        $Query = $this->input->post('q');
        // set document information
        $this->pdf->SetAuthor('PrimexOne Exchange');
        $filename = $this->makefilename('pdf','DSP');
        $this->pdf->SetTitle('Daily Sales Report - '.date('d/m/Y'));
        $this->pdf->SetSubject('PrimexOne Sales Report');
        $this->pdf->SetKeywords('keywords');        
        $this->pdf->SetFont('helvetica', 'N', 12);   
        // add a page
        //$this->pdf->AddPage();     
        // write html on PDF
        $this->pdf->Cell(0,10,'Welcome in PrimexOne');

        //Creating FileName

        $filepath = APPPATH.'cache/pdfcache/';
        $fullname = $filepath.$filename;
        $this->pdf->Output($fullname, 'F');     
        $this->downloadfile(array('format'=>'pdf','filename'=>$filename,'filepath'=>$fullname));            
    }

ajax and PHP perform perfectly and created PDF in pdfcache folder, problem is it's not downloading files when file created.

here is my downloading script

    public function downloadfile($arr)
    {

         $mime = 'application/force-download';
           header('Pragma: public');    
           header('Expires: 0');        
           header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
           header('Cache-Control: private',false);
           header('Content-Type: '.$mime);
           header('Content-Disposition: attachment; filename="'.$arr['filename'].'"');
           header('Content-Transfer-Encoding: binary');
           header('Connection: close');
           readfile($arr['filepath']);    
           exit();

    }   

hope you Genius understand my problem

HeartDisk
  • 47
  • 1
  • 9
  • 1
    possible duplicate of [Download file through an ajax call php](http://stackoverflow.com/questions/6668776/download-file-through-an-ajax-call-php) – stormdrain Sep 05 '13 at 13:53

2 Answers2

1

application/force-download is not a mime type.

Try

$mime = 'application/pdf';

$('.exporter').bind('click',function(){
    var Query = $('#exportQuery').val();
    var callfunction = 'reportprints/print_'+$(this).attr('id').toLowerCase();
    event.preventDefault();
    var newForm = $('<form>', {
        'action': callfunction,
        'target': '_top'
    }).append($('<input>', {
        'name': 'q',
        'value': Query,
        'type': 'hidden'
    }));
    newForm.submit();
});
stormdrain
  • 7,915
  • 4
  • 37
  • 76
  • i did now it's showing %PDF-1.7 7 0 obj << /Type /Page /Parent 1 0 R /LastModified (D:20130905152441+02'00') /Resources 2 0 R /MediaBox [0.00 0.00 595.28 841.89] /CropBox [0.00 0.00 595.28 841.89] /BleedBox [0.00 0.00 595.28 841.89] /TrimBox [0.00 0.00 595.28 841.89] /ArtBox [0.00 0.00 595.28 841.89] /Contents 8 0 R /Rotate 0 /Group << /Type /Group /S /Transparency /CS /DeviceRGB >> /Annots [ 5 0 R ] /PZ 1 >> endobj 8 0 obj etc etc – HeartDisk Sep 05 '13 at 13:25
  • Yeah, this isn't going to work (I forgot about the "ajax" part when I answered) without a library or iframe. http://stackoverflow.com/q/1999607/183254 http://stackoverflow.com/q/6668776/183254 There a some good and simple solutions in these links. – stormdrain Sep 05 '13 at 13:52
  • Forbidden You don't have permission to access /application/cache/pdfcache/DSP-abuzainab-20130905170415.pdf on this server. what should i don know – HeartDisk Sep 05 '13 at 15:05
  • Happy to help. If you still have problems with it (and can't find the solution on google or stackoverflow), just ask another question :) – stormdrain Sep 05 '13 at 15:22
0

Try This

window.location.replace('Your URL Here')

And pass the required variables in get.

It will look like normal but works perfectly.

Rajasekhar
  • 517
  • 1
  • 4
  • 14
  • I have used download library by CI for downloading the file in called function. It will be very easy to use you have to pass file name for downloading pdf and path to pdf `code`force_download('Req File Name.pdf', 'Path to pdf');`code` – Rajasekhar Sep 06 '13 at 06:49