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 4 of 4
  1. #1
    Regular Coder
    Join Date
    May 2011
    Posts
    357
    Thanks
    23
    Thanked 1 Time in 1 Post

    Dowload Function

    Is there a way to make a function that downloads a file to a computer? The file would just be text.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    AJAX


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,092
    Thanks
    23
    Thanked 594 Times in 593 Posts
    The simplest way to emulate an HTML download tag is to simply tell people to right-click on the link to download the file. When you right click a link one of the options on the context menu is a "Save As..." or "Save link as...". When you choose that, the file on the other side of the link is downloaded to the hard drive, even if it can be opened in the browser.

    A simple download message:
    Right click the link below and choose "Save As..." to download this file.

  • #4
    New Coder
    Join Date
    Mar 2010
    Posts
    30
    Thanks
    0
    Thanked 1 Time in 1 Post
    This must be solved on the server side. In order for the browser to prompt the user to save the file to disk, the proper headers will need to be set. The following is an example using PHP to force a .txt file to be saved to disk.

    PHP Code:
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=text.txt");
    header("Content-Type: text/plain");
    header("Content-Transfer-Encoding: binary");
        
    readfile('text.txt'); 


  •  

    Posting Permissions

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