如何链接到浏览器无法查看的文件?

时间:2022-05-27 08:08:42

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。我希望访问我网站的访问者能够在点击链接时下载该文件。链接应该是这样的:下载我的程序。在我的webserver上,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?

但是如果我只是用标签,浏览器不会识别链接,对吗?因为浏览器不能打开zip文件。那么连接到它的正确方式是什么呢?

1 个解决方案

#1


11  

Actually, your example is the 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 Reader and Schubert’s PDF Browser Plugin that 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.

这遵循了web的原则,即您不知道浏览器将如何处理您发送的文件和页面。您提到了ZIP文件,但是请考虑PDF文件。它们就像一个ZIP文件:它们不是HTML,不是为浏览器而设计的,浏览器可能会下载它。但也有一些插件,如Adobe PDF阅读器和舒伯特的PDF浏览器插件,可以在浏览器中显示PDF文件的内容。同样,假设浏览器可能有一个ZIP文件查看器——它可能向用户显示浏览器中的ZIP文件的内容,并让用户决定从哪里提取这些内容。

Most browsers don’t have 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标头设置系统。例如,如果你有一个ZIP和一个RAR版本的my-program,那么你可以让它链接到my-program,浏览器选择它最喜欢的版本。但是设置起来可能会很复杂,而且这种系统通常不需要仅仅下载一个文件。Accept标头通常用于获得浏览器想要查看的内容的正确版本——例如,浏览器可能会选择MP4视频文件而不是WMV视频文件,因为它没有任何可以播放嵌入式WMV视频的编解码器。


If you want to force the browser to download a file even though the browser can probably view it on its own, see this question.

如果您想强迫浏览器下载一个文件,即使浏览器可能可以自己查看它,请参见这个问题。

#1


11  

Actually, your example is the 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 Reader and Schubert’s PDF Browser Plugin that 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.

这遵循了web的原则,即您不知道浏览器将如何处理您发送的文件和页面。您提到了ZIP文件,但是请考虑PDF文件。它们就像一个ZIP文件:它们不是HTML,不是为浏览器而设计的,浏览器可能会下载它。但也有一些插件,如Adobe PDF阅读器和舒伯特的PDF浏览器插件,可以在浏览器中显示PDF文件的内容。同样,假设浏览器可能有一个ZIP文件查看器——它可能向用户显示浏览器中的ZIP文件的内容,并让用户决定从哪里提取这些内容。

Most browsers don’t have 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标头设置系统。例如,如果你有一个ZIP和一个RAR版本的my-program,那么你可以让它链接到my-program,浏览器选择它最喜欢的版本。但是设置起来可能会很复杂,而且这种系统通常不需要仅仅下载一个文件。Accept标头通常用于获得浏览器想要查看的内容的正确版本——例如,浏览器可能会选择MP4视频文件而不是WMV视频文件,因为它没有任何可以播放嵌入式WMV视频的编解码器。


If you want to force the browser to download a file even though the browser can probably view it on its own, see this question.

如果您想强迫浏览器下载一个文件,即使浏览器可能可以自己查看它,请参见这个问题。