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 2 of 2

Thread: js to download

  1. #1
    Regular Coder
    Join Date
    Aug 2012
    Posts
    121
    Thanks
    18
    Thanked 0 Times in 0 Posts

    js to download

    I have a download section in my website where I will put my documents for user download. I want the user only left mouse click to the link then his browser will prompt the download screen tosave file.

    Is it possible to do in HTML/JavaScript?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,216
    Thanks
    75
    Thanked 4,344 Times in 4,310 Posts
    HTML. Nothing to do with JavaScript, really. Just a simple link:
    <a href="somefile.xxx"> download somefile </a>

    But if you don't want the file to be opened *in* the browser (e.g., say it is a PDF file, which would normally just open up right in the browser; or an MS Word file opening in MSIE browser), then you have to alter its HTTP content information, and that means server-side coding. For example, with PHP you could do
    Code:
          header('Content-type: application/pdf');  
          header("Content-Disposition: attachment; filename=\"xxx.pdf\"");
    (Similar code with other server-side systems.) And then serve up the file in your HTML href link as a PHP file.

    ** EXCEPTION **
    *IF* the browser is HTML5 compatible, then this should work:
    <a href="somefile.pdf" download> download some pdf file </a>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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