Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

link to download file as opposed to open file

Options
  • 04-09-2007 2:35pm
    #1
    Registered Users Posts: 648 ✭✭✭


    hi,

    i have a link on a site to a .txt file. the link currently opens the file in the browser - however i want it to download the file.

    anyone know how to do this?

    tnx


Comments

  • Closed Accounts Posts: 15,914 ✭✭✭✭tbh


    right click the link - > save target as / save link as (depending on your browser)


  • Registered Users Posts: 2,299 ✭✭✭PixelTrawler


    I presume you're left clicking.

    Just right click and select "Save as..."
    If its your site, print in italics underneath or beside the link "right click and select save as..." so that users know to do this.

    I guess the other alternative would be to write a small script that would present the file for downloading on left clicking...


  • Registered Users Posts: 648 ✭✭✭ChicoMendez


    I presume you're left clicking.

    Just right click and select "Save as..."
    If its your site, print in italics underneath or beside the link "right click and select save as..." so that users know to do this.

    I guess the other alternative would be to write a small script that would present the file for downloading on left clicking...


    how do i write one of those scripts ?
    id rather provide an easier solution than right click and save as - if it exists !
    tnx


  • Registered Users Posts: 3,594 ✭✭✭forbairt


    if you've got php ... something like ...
    <?php
    
    header('Content-type: text/plain');
    
    header('Content-Disposition: attachment; filename="myfile.txt"');
    
    readfile('myfile.txt');
    ?> 
    

    something like this would do it for you ...


  • Registered Users Posts: 648 ✭✭✭ChicoMendez


    great thanks

    does anyone know is there an apache command that can be used so as all .txt or .lic files clicked on the site will be treated as a download (as opposed to open new window) ?


    tnx


  • Advertisement
  • Registered Users Posts: 648 ✭✭✭ChicoMendez


    in fact something strange is happening
    on a webpage i have two links

    one to a file called a.lic : this opens in the browser

    another called b.lic : this opens a download dialogue



    the content of a.lic is =

    BEGIN LICENCE
    PccOxzITrOk+NBsET9rxMzU+7c8KZKraHTudBy8VKGqTa5RRJFU6uiEihTHreE9o
    N3YsOcWKoJV8lgqwNB+0kErJ1kKiyQ8tKqRreSxY8CFyCpru/dbJ4NM5WUyzHOXG
    u1IOPtruKNLbNVlIuIF9S2q+Sn9Hwpb54m2f9Ej4MZ48P3htbCB2ZXJzaW9uPSIx
    LjAiIGVuY29kaW5nPSJ1dGYtNyI/PjxTa3lSZWNvbj48UHJvZHVjdCBPd25lcj0i
    enp6IiBJRD0iYzM5NmY4ZTQtYWNlNi0xMDJhLWIxMDUtMDAzMDQ4OTAwOTYwIiBF
    bXBpcnVtQmFzaWM9Ik9uIiBNYXN0ZXI9IjEiIFNsYXZlPSIxIiBFbmRQZXJpb2Q9
    IjAxLzA3LzIwMDgiIC8+PC9Ta3lSZWNvbj4=
    END LICENCE

    the content of b.lic is =

    r,YIB]SX^BkHBe ,(@K=FPSO,1)IR#Q_(SX?ATCV,7*YDV"YNB*=Ke


    anyone know why they would have different behaviour ?

    tnx


  • Registered Users Posts: 3,594 ✭✭✭forbairt


    you'll want to add in the following to your httpd.conf
    <FilesMatch "\.(?i:txt)$">
      ForceType plain/txt
      Header set Content-Disposition attachment
    </FilesMatch>
    

    You'll also need to include the headers module mod_headers.so


  • Registered Users Posts: 4,468 ✭✭✭matt-dublin


    try:
    put this somewhere between <head> </head>
    <script type="javascript">
    function saveFile (fname)
    {
    document.execCommand('SaveAs',null,fname)
    }
    </script>

    You can use a button

    <input type="button" value="Save" onClick="saveFile('thispage.htm');">

    or text

    <a href="javascript:null()" onClick="saveFile('thispage.htm');">Save
    File</a>


  • Registered Users Posts: 648 ✭✭✭ChicoMendez


    forbairt wrote:
    you'll want to add in the following to your httpd.conf
    <FilesMatch "\.(?i:txt)$">
      ForceType plain/txt
      Header set Content-Disposition attachment
    </FilesMatch>
    

    You'll also need to include the headers module mod_headers.so

    thanx man that sorted it!


  • Registered Users Posts: 3,594 ✭✭✭forbairt


    cool ... you could also add it to the .htaccess for a specific folder which might be a better idea depending on what else will be happening on your server


  • Advertisement
  • Subscribers Posts: 9,716 ✭✭✭CuLT


    Dear God look at all those horrible javascript and php versions of a simple htaccess rule.


  • Registered Users Posts: 3,594 ✭✭✭forbairt


    CuLT wrote:
    Dear God look at all those horrible javascript and php versions of a simple htaccess rule.

    I did offer both ... php and .ht...

    php version allows you to check who the user is first and what permissions they have ... (of course in this case ... that isn't required ...) and you could do .htpasswd ... auth ... but ... hmm... :)

    you won't always have access to apache ... or .htacess files depending on your hosting provider whether its your own server or not and so on ...


Advertisement