Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   JavaScript and Client-Side Browser Scripting (http://www.programmingforums.org/forum23.html)
-   -   download dialog on button click (http://www.programmingforums.org/showthread.php?t=13186)

kruptof May 21st, 2007 2:50 PM

download dialog on button click
 
Does any body know whether it's possible to display the download dialog box, when the user clicks a button, if so can you show me the code or just explain how to go about it.

Edit: this is in javascript

mackenga Jun 10th, 2007 5:21 AM

Technically it's up to the browser when (and if ever) it displays the download dialog box. Most browsers will do something like it when they find they're trying to load something they can't display themselves or with help, based on the MIME type of what's coming in. So really what you want to do is point the browser at the data to be downloaded, and provided a link to the file where you want a button works, that's no problem.

A trivial version of this that probably won't be exactly what you're after but might form part of the basis for it could be:

:

<INPUT type="button" onClick="document.location.href = somefile.zip" ...>

As usual with my code examples that's very minimal and I haven't actually tested it but that's the general idea - just convince the browser to try to start loading the file. A word of warning though - not all browsers run JavaScript so while a simple link would do the job for everyone, this will shut out some users. This is probably not as significant as it used to be, but I for one do find myself using elinks from time to time and I'm sure I'm not the only one!

There are two ways to do something similar in a way that works for everyone; one is a link that is an image that looks like a button - you can use its onMouseOver and onClick events to animate it to make it really convincing and if it's still essentially a simple hyperlink and you supply some alt text for the image it'll work for everyone. Another option is to use a form that submits to a CGI that produces the data - this should work for everyone too. For example, lynx won't handle the button with onClick (no JavaScript) but it does handle Submit, which will be rendered as a button in most browsers but handled in sensible other ways by browsers that don't do buttons.

I haven't done any serious web development in years and generally like to keep things as minimal as possible for portability so I hope this isn't creaking and ancient information now! If I was faced with this challenge, to be honest I'd just use a text link that would work for everyone and maybe try to tart it up with some CSS to make it look nicer where that was supported. I got really sick of trying to make clever hacks work in IE as well as standard browsers a good while ago, which is why my knowledge of 'fancy' web development might be aging ungracefully these days.

Anyway, hope this helps.


All times are GMT -5. The time now is 2:14 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC