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 7 of 7
  1. #1
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Question Give download link in same window?

    I'm trying to allow a user to download a file off my server without opening a new page. How would I do that with JavaScript?

    Thanks!

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,313
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by Mooseman View Post
    I'm trying to allow a user to download a file off my server without opening a new page. How would I do that with JavaScript?

    Thanks!
    Only the user can determine whether a new page opens in the same window, a new window or a new tab. You cannot control or dictate that.

    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
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by Philip M View Post
    Only the user can determine whether a new page opens in the same window, a new window or a new tab. You cannot control or dictate that.
    I understand I can't force a pop-up. But that is not what I am asking. How do I give the user a 'save as' dialog (when they click my button) without opening a new window/new tab/same tab?

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Simply provide a link to the file you want them to be able to download and make sure the file is set up so it downloads rather than trying to display in the browser.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by felgall View Post
    Simply provide a link to the file you want them to be able to download and make sure the file is set up so it downloads rather than trying to display in the browser.
    I have that currently and it works in Chrome, but it doesn't work in IE. Is there something I can change, or is it an IE thing?

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    It is completely independent of the browser so if it works in one browser but not another then it means you are not specifying it correctly to force it to download.

    If you don't have access to specify that the file must be offered for download from the server then offer it as a zip file or some other similar format that all browsers do not attempt to process within the browser.

    If you do have access to specify it on the server then you simply need to set Content-Disposition:attachment for the file using whatever server side options available that allow you to set it.
    Last edited by felgall; 04-18-2012 at 11:12 PM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • Users who have thanked felgall for this post:

    Mooseman (04-19-2012)

  • #7
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Smile

    Quote Originally Posted by felgall View Post
    If you don't have access to specify that the file must be offered for download from the server then offer it as a zip file or some other similar format that all browsers do not attempt to process within the browser.
    No, I don't have access to the server, but using .zip files works in IE. Thanks!


  •  

    Tags for this Thread

    Posting Permissions

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