Hello and welcome to our community! Is this your first visit?
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
    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. #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Thanked 4,947 Times in 4,908 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
          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>
    Be yourself. No one else is as qualified.


Posting Permissions

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