How can I get browsers to download an exe instead of opening it in the browser window?

断了今生、忘了曾经 提交于 2019-12-13 19:01:25

问题


I have a website from which I want to enable browser users to download an exe.

I am testing it with a very simple HTML file which I load into the web browser with "File>Open". The "body" looks something like this:

<body>
<a href="http://www.example.com/myprogram.exe" target="_self">click to download exe</a>
<a href="http://www.example.com/myprogram.zip" target="_self">click to download zip</a>
</body>

The issue is this: when I click on the first link (myprogram.exe), the browser (IE8 as well as FireFox) streams the myprogram.exe directly into the browser window: I see a whole lot of binary.

When I click on the second link (myprogram.zip), the browser asks me if I would like to open the file or save it to disk, which is what I expected to happen with the .exe.

This is the ONLY .exe download that causes this strange behaviour. I have downloaded .exes often and the browsers always ask me whether to open the file or save to disk.

Is there something that needs to be set on the web site itself, or the host or ... ?


回答1:


You need to correct the content-type your webserver is sending. It sounds like it is claiming that the data is text/plain. My mime.types file suggests exe files should be application/x-msdos-program

If you are using Apache, see http://httpd.apache.org/docs/1.3/mod/mod_mime.html#addtype (or the similar page in the manual for the version you are using).




回答2:


You need to set the Content-Disposition HTTP header.

UPDATE: The HTTP headers are typically controlled in the web server, e.g., Apache.

As another poster mentions, most browsers should download .exe files as an attachment automatically if the server is sending the correct Content-Type header. How to do this varies from server to server. Here's an article on setting MIME types (another name for content-type) in IIS. In Apache, it is typically done by editing the file your TypesConfig directive points to.




回答3:


See this

How To Raise a "File Download" Dialog Box for a Known MIME Type

When you serve a document from a Web server, you might want to immediately prompt the user to save the file directly to the user's disk, without opening it in the browser. However, for known MIME (Multipurpose Internet Mail Extensions) types such as Microsoft Word ("application/ms-word"), the default behavior is to open the document in Internet Explorer.

You can use the content-disposition header to override this default behavior. Its format is:

Content-disposition: attachment; filename=fname.ext




回答4:


In the end it required AddType application/x-octet-stream exe being applied.

Thanks to all the answerers who pointed me that way.



来源:https://stackoverflow.com/questions/1502960/how-can-i-get-browsers-to-download-an-exe-instead-of-opening-it-in-the-browser-w

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!