Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
  1. #1
    New to the CF scene
    Join Date
    Oct 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to download file from ajax response?

    Hi every one,

    I am working on web based one page application.
    Only accept ajax request.
    I want to download file. I managed to send request to server,
    on server i got string data from get_file_content,
    Client side in ajax success, i can see binary data in console.

    Problem: I am not able to save file. i need a help how to download file from server via ajax request. I hope i explained my issue well.

    I tried alot, Blob is not working, dont know why, when i create blob from response data file saved. when i open, it says no image, or in pdf file corrupted.

    Is there best way to do...

    Code:
    var ajaxUrl = "<?php echo base_url() ?>download_legal_document/" + file_name;
                console.log(ajaxUrl);
                var ajaxObj = $.ajax({
                    method: 'POST',
                    url: ajaxUrl,
                   processData: false,             
                    cache: false,
                    xhrFields: {responseType: 'binary'},
                    beforeSend: function (xhr) {
                    },
    
    success: function (data, status, xhr) {
                        console.log(data);
                        console.log(status);
               
                        console.log(xhr.getResponseHeader("content-type"));
                        blob = new Blob([data], {type: '' + xhr.getResponseHeader("content-type")});
    
                        a = $('<a />'), url = URL.createObjectURL(blob);
                        a.attr({
                            'href': url,
                            'download': file_name,
                            'text': "click"
                        }).hide().appendTo("body")[0].click();
                        
    
                    }
    Above code save file but no data,

    PHP


    Code:
    $filedata = @file_get_contents($path_to_file)
    
     if ($filedata) {
                // GET A NAME FOR THE FILE
                $basename = basename($filename);
    
                // THESE HEADERS ARE USED ON ALL BROWSERS
                header("Content-Type: application-x/force-download");
                header("Content-Disposition: attachment; filename=$file_name");
                header("Content-length: " . (string) (strlen($filedata)));
                header("Expires: " . gmdate("D, d M Y H:i:s", mktime(date("H") + 2, date("i"), date("s"), date("m"), date("d"), date("Y"))) . " GMT");
                header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    
                // THIS HEADER MUST BE OMITTED FOR IE 6+
                if (FALSE === strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE ')) {
                    header("Cache-Control: no-cache, must-revalidate");
                }
    
                // THIS IS THE LAST HEADER
                header("Pragma: no-cache");
    
                // FLUSH THE HEADERS TO THE BROWSER
                flush();
    
                // WRITE THE FILE
                echo $filedata;
            }
    Regards.
    Last edited by vinyl-junkie; Oct 3rd, 2018 at 12:29 AM. Reason: added code tags


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •