How do I make a button that will bring up the download box for something on my website, without the user having to right-click and select "Save Target as..." :?:
Thanks
:lol: Martin :lol:

12345\rock commented: 12456\rock +0

Recommended Answers

All 25 Replies

You can't. This is fundamental "user interface" 101. A programmer shouldn't attempt to force actions that are historically under user control. Also, what you're attempting violates the browser security model. Just imagine the havoc that would be caused if a web page could "silently download" anything as soon as you browse to it.

I understand you simply want the dialog to pop up, but it's still not allowed.

Oh well...
Thanks anyway
:cool:

The future is web applications, and needs to provide the same capabilities as a WinForms, if the developer deems so. 90% of my sites are intranet based, so I do not have the fear of anonymous users. If you are using the .NET framework you have some options. You have the ability to create a server-side method or page dedicated to writing the document to the response stream. This will cause the parent page to postback, but it will prompt for download. If you are looking to do this without postback you this can achieved using AjaxPro, http://ajaxpro.schwarz-interactive.de, or other any other Ajax frameworks.

What ??? - sorry I'm not with that at all can you explain again please?

Martin

P.S. I wanted to make a link on my website so that when someone clicked on it a .zip file would download - to save them right clicking and clicking on "Save Target as..." etc.

It can't be done with HTML, CSS or JavaScript, the topics covered in this forum. What akadez is referring to are some possible techniques in ASP.NET, a server-side language. Daniweb has an ASP.NET forum, if you code in that language.

Most of your visitors will have .zip files set to offer as download rather than process automatically so you could upload in that format. There is also code that can be put in the .htaccess file to force what you want if your site is running on an apache server.

You can't. This is fundamental "user interface" 101. A programmer shouldn't attempt to force actions that are historically under user control. Also, what you're attempting violates the browser security model. Just imagine the havoc that would be caused if a web page could "silently download" anything as soon as you browse to it.

I understand you simply want the dialog to pop up, but it's still not allowed.

Excuse me but the idiot has arrived :o with a question :?: about your answer. Pages seem to download a lot of crap onto my machine when I browse the internet. I have gotten worms, spyware, and other malicious stuff without ever asking for it nor agreeing to it. *thinks I need to hide under the bed now as you are all probably dying laughing* but I really want to know. When you stop laughing at me will you please explain this to me? :rolleyes:

I don't mind the question, but you're asking it in the wrong spot! Daniweb has a forum specifically for these types of issues: http://www.daniweb.com/techtalkforums/forum64.html

If you'll peruse that forum, I think you'll get the education you seek! If not, feel free to post a new thread there. No one will bite your head off; we try to be a friendly group.

Wait wait wait ... what is being asked is a client-side technology, not server-side at all ... Anything that would interact directly with the web browser - such as opening a dialog menu, would have to be a client-side script.

Server side scripts can interact with files on the server, but you need something client side to interact with files or applications (in this case, the web browser) on the client side of things.

I think this could be accomplished in DHTML (although I'm not sure specifically how to go about it), can it not?

Update, I searched the web and found out how to do it in JavaScript:

<a href="#" onclick="javascript:document.execCommand('SaveAs','1',null);">Link Here</a>

Replace null with a filename (if you use frames, or if you want clicking the link on one page to save another one).

I don't mind the question, but you're asking it in the wrong spot! Daniweb has a forum specifically for these types of issues: http://www.daniweb.com/techtalkforums/forum64.html

If you'll peruse that forum, I think you'll get the education you seek! If not, feel free to post a new thread there. No one will bite your head off; we try to be a friendly group.

Sorry but your answer just struck a cord in me because I just had such a bad worm that my antivirus company sent me a custom cleaner which killed all my file associations. I did figure out how to get them back. . well most of them . . but I still need to reformat. The guy who sent the custom cleaner when I called him bawling my head off told me that it was something I got from visiting a website. He looked through my history to try and determine where I got it and how and finally concluded that it was just dumped on me. And they said that they were glad that I had called them with info at hand because others would be calling soon as well. You said that guy couldn't do something that I am not sure that I understand because :

what you're attempting violates the browser security model. Just imagine the havoc that would be caused if a web page could "silently download" anything as soon as you browse to it.

But my antivirus guy says that it is why we get viruses and worms and other nasty things on our pc's. So I guess that what I am really asking is what is the real truth here? I don't need help, I want understanding.


Thanks for not laughing too hard at me tgreer.

The code I provided will pop up with a message box when a link is clicked on. It is similar to those links that say "Click here to add this site to your Favorites / Bookmark list" ... only this one is a "Click here to download this page"

cscgal - the code you provided is IE only, and won't work with strict doctypes.

Bomba - spyware works by sneaking in a program that runs concurrently with or as an extension to the browser. It usually works by the user actually installing the spyware, through accepting a page that wants to install a "plug-in" of some sort. Then whenever the browser starts, the spyware starts. That topic really is best explored in the other forum, this one is to discuss client-side programming.

Original Poster - there is no way, as I explained, to code a button that downloads a file, in a completely client-side manner (aside from the IE-only activeX trick).

The proper way to do this is to stream back the proper mime-type. That's server configuration and server-side programming, not client-side. If the user has his browser configured to support that mime-type with a helper application (think "Acrobat Reader" or "Flash"), the stream will be passed to that application. Otherwise, the user will be asked what they want to do with the stream being served. That's the way the web works.

I'm sorry folks! I misread the question. Ignore me :) What I had THOUGHT you wanted was a link that duplicated the Internet Explorer function of selecting "Save As" from the File menu. I completely didn't notice the word "target" for some reason. I shouldn't post too early in the morning anymore.

Thanks to everyone who has answered this Thread.

Sorry I haven't replied recently, I changed my e-mail address and didn't update my DaniWeb Account. :lol:

Thanks Again, You've helped a lot!!! :D

Martin

You are wanting to allow the user to download a ZIP file from your website? By choice, and by link/button?

Simple, instead of the link pointing to filename.html, use filename.zip. User will get an option to save or run the file.

The same for any file name the browser doesn't recognize.
If it cant render it, it downloads it.

(I didnt read the entire thread on this one, just gave a seemingly-simplified answer.)

When I click on a link, in other websites, to a .zip file it downloads in the background of the webpage with no warning - only a picture of an arrow pointing to a hard disk in the status bar. If the user has a slow connection it can seem like nothing is happening. When it has downloaded, the temporary file where it has saved opens.

I wanted to do the equivalent of right clicking the link and clicking "Save Target As..." - this brings a dialog box saying save or open the file.

:lol: Martin :lol:

When I click on a link, in other websites, to a .zip file it downloads in the background of the webpage with no warning - only a picture of an arrow pointing to a hard disk in the status bar. If the user has a slow connection it can seem like nothing is happening. When it has downloaded, the temporary file where it has saved opens.

I wanted to do the equivalent of right clicking the link and clicking "Save Target As..." - this brings a dialog box saying save or open the file.

:lol: Martin :lol:

I think that may be do to your sys settings.

Maybe "tools|Internet Options|Advanced Tab"

I've used every windows system exceot 2000 and they've always opened a direct download dialog when clicking any file-type that IE cant play itself. So I'm really curious about your situation...I would want to know "for sure" when I'm DLing....lol.....good luck.

I mainly use Firefox instead of IE now, so I don't have the problem anymore :lol:

Firefox is good, but some parts of websites that work on IE don't work on it - I think that's it's only negative point.

Martin

this can be done in php by using the header function, without the need of javascript and it works in safari, internet explorer, mozilla (firefox), etc...

since you are using an html forum I'm assuming that this wasn't what you were looking for though

this can be done in php by using the header function, without the need of javascript and it works in safari, internet explorer, mozilla (firefox), etc...

since you are using an html forum I'm assuming that this wasn't what you were looking for though

No, it's fine.

How would I make a download dialogue box appear when a user opens a certain page using PHP?

Thanks
Martin

$file is the actual file (with the path included)
$new_filename is the filename that you want to send to the user. So when they download this will be the name used by the browser

the first 3 headers force the browser to download the file each and every single time, eg it's not pulling it from the memory/dish cache

the content type is just sent as some binary data type
the content-disposition tell the browser to force a download, do not open in the browser, even if you have a plugin to open it.

i use this script for images, word documents, pdfs and it works just fine.

if (is_file($file))
{
	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
	header("Cache-Control: no-store, no-cache, must-revalidate");
	header("Pragma: no-cache");

	header("Content-type: application/octet-stream");
	header("Content-Disposition: attachment; filename=".$new_filename);
	readfile($file);
	exit();
}

Sorry, I'm quite new to PHP and I'm not sure how I'd go about putting this script onto my site.

Could you give me a working example of this?

Thanks
Martin

I'd like to close this thread - it's drifting too far. Please start a new thread in the PHP forum.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.