Html 如何链接到浏览器无法查看的文件进行下载?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11252389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I link to a file to download that a browser can’t view?
提问by Rory O'Kane
In HTML, how do you link to a file that the browser can only download, not view? For instance, say I have a zip file, my-program.zip. I want visitors to my website to be able to download that file when they click a link. The link would look like this: download my program. On my webserver, the HTML file and the zip file are in the same directory, so the relative path of the zip file is simply its filename.
在 HTML 中,如何链接到浏览器只能下载而不能查看的文件?例如,假设我有一个 zip 文件my-program.zip。我希望我网站的访问者在单击链接时能够下载该文件。该链接将如下所示:下载我的程序。在我的网络服务器上,HTML 文件和 zip 文件位于同一目录中,因此 zip 文件的相对路径就是其文件名。
But if I just link to the file with an <a href="my-program.zip">
tag, the browser wouldn't recognize the link, right? Because browsers can't open zip files. So what is the proper way to link to it?
但是如果我只是用<a href="my-program.zip">
标签链接到文件,浏览器将无法识别该链接,对吗?因为浏览器无法打开 zip 文件。那么链接到它的正确方法是什么?
回答by Rory O'Kane
Actually, your example isthe proper way to link to the file:
实际上,您的示例是链接到文件的正确方法:
<a href="my-program.zip">download my program</a>
You can never tell for sure that a browser can't view a file. You just link to it; it's up to the browser to do what they think best with it – display it, download it, or do something else. Don't worry; browsers will generally do the right thing.
您永远无法确定浏览器无法查看文件。你只需链接到它;浏览器可以用它来做他们认为最好的事情——显示它、下载它或做其他事情。别担心;浏览器通常会做正确的事情。
This follows the principle of the web that you don't know what the browser will do with the files and pages you send it. You mentioned a ZIP file, but think of PDF files. They are like a ZIP file: they are not HTML, they are not made for a browser, and the browser might download it. But there are plugins such as Adobe PDF Readerand Schubert's PDF Browser Pluginthat show the contents of the PDF file right in the browser. Similarly, hypothetically, there might be a ZIP file viewer for the browser – it might show the user the contents of the ZIP file in the browser and let the user decide where to extract those contents.
这遵循网络的原则,即您不知道浏览器将如何处理您发送的文件和页面。您提到了 ZIP 文件,但想到了 PDF 文件。它们就像一个 ZIP 文件:它们不是 HTML,它们不是为浏览器制作的,浏览器可能会下载它。但是有一些插件,例如Adobe PDF Reader和Schubert 的 PDF Browser Plugin,可以直接在浏览器中显示 PDF 文件的内容。同样,假设浏览器可能有一个 ZIP 文件查看器——它可能会在浏览器中向用户显示 ZIP 文件的内容,并让用户决定在哪里提取这些内容。
Most browsers don'thave the hypothetical ZIP file viewer described, so the file will just download, like you wanted. But that doesn't really matter; just write your link and everything will be okay.
大多数浏览器都没有描述的假设 ZIP 文件查看器,因此文件会像您想要的那样下载。但这并不重要;只需写下您的链接,一切都会好起来的。
The browser could do things other than viewing the file or downloading the file right away. It could also ask the user if they want to download the file. Or it could start downloading the file, detect a virus in it, and delete it right away. The point is, it's up to the browser what it does with the file.
除了查看文件或立即下载文件之外,浏览器还可以执行其他操作。它还可以询问用户是否要下载文件。或者它可以开始下载文件,检测其中的病毒,然后立即将其删除。关键是,这取决于浏览器对文件的处理方式。
Note that this policy goes the other way. Your HTML pages look to the browser just like files look – they are both “resources”. “Resource” is the “R” in “URL”. When you visit an HTML page by visiting a URL, the browser thinks “this is an HTML resource. What should I do with this? Oh, I can display it in the main window – I'll do that.” This is the same process as downloading a ZIP file after clicking a link to its URL, where it thinks “this is a ZIP resource. What should I do with this? I can't display it – I guess I'll start downloading it and open the downloads window so the user can see what happened.” Most browsers even let you download the HTML of a page just like a file, if you ask it to.
请注意,此政策是相反的。你的 HTML 页面在浏览器中看起来就像文件一样——它们都是“资源”。“资源”是“URL”中的“R”。当您通过访问 URL 来访问 HTML 页面时,浏览器会认为“这是一个 HTML 资源。我该怎么办?哦,我可以在主窗口中显示它——我会这样做的。” 这与在单击指向其 URL 的链接后下载 ZIP 文件的过程相同,它认为“这是一个 ZIP 资源。我该怎么办?我无法显示它——我想我会开始下载它并打开下载窗口,这样用户就可以看到发生了什么。” 如果您要求,大多数浏览器甚至允许您像下载文件一样下载页面的 HTML。
If you have multiple formats of your file, and want to let the browser choose the best one it can view, then you could set up a system using the HTTP Accept header. For instance, if you had both a ZIP and a RAR version of my-program, then you could make it so you link to just my-program
and the browser chooses the version it likes best. But the setup for that can be complicated, and that kind of system isn't usually necessary for just a file download. The Accept header is usually used to get the correct version of something that the browser is meant to view– for instance, the browser might choose an MP4 video file over a WMV video file because it doesn't have any codec that can play embedded WMV videos.
如果您的文件有多种格式,并且希望让浏览器选择它可以查看的最佳格式,那么您可以使用HTTP Accept 标头设置系统。例如,如果您同时拥有my-program的 ZIP 和 RAR 版本,那么您可以将其设置为链接到my-program
,浏览器会选择它最喜欢的版本。但是为此设置可能很复杂,而且这种系统通常不是仅用于文件下载所必需的。Accept 标头通常用于获取浏览器要查看的内容的正确版本- 例如,浏览器可能会选择 MP4 视频文件而不是 WMV 视频文件,因为它没有任何可以播放嵌入式 WMV 的编解码器视频。
If you want to force the browser to download a file even thoughthe browser can probably view it on its own, see this question.
如果您想强制浏览器下载文件,即使浏览器可能可以自行查看,请参阅此问题。