SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2010
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Force download file from a page loaded by ajax

    Why isn't my code works when im trying to force download file inside a page which im loading with ajax?

    this is the page code which im loading with ajax into another page:
    PHP Code:
     $file='file.jpg';
          
    // Set headers
         
    header("Cache-Control: public");
         
    header("Content-Description: File Transfer");
         
    header("Content-Disposition: attachment; filename=$file");
         
    header("Content-Type: application/zip");
         
    header("Content-Transfer-Encoding: binary");
        
         
    // Read the file from disk
         
    readfile($file); 

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,786
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Forcing a file to download (using the headers like you have there) is done simply by linking to the file from within a web page. No ajax is required.

    If you use ajax to read that file then most of the headers will be ignored as ajax returns the page for the JavaScript to process it further and all you can do with the headers from there is to examine their content - usually you'd only have a content type header for a file that is being read from ajax.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    Foozle Reducer ServerStorm's Avatar
    Join Date
    Feb 2005
    Location
    Burlington, Canada
    Posts
    2,699
    Mentioned
    89 Post(s)
    Tagged
    6 Thread(s)
    Ajax will not work here (as Fegall mentioned);

    This is either done directly in the php page say if a $_POST variable is set on a self posting form then you trigger the download code (before any content is echoed). Another technique is to load within an I-FRAME although I won't show you how to do this as I don't like this way of doing things - makes accessibility a nightmare. By combining javascript and an I-FRAME you can get AJAX like functionality.
    ictus==""


Bookmarks

Posting Permissions

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