如何从 HTML 打开外部文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/101574/
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 to open an external file from HTML
提问by JosephStyons
I want a list of hyperlinks on a basic html page, which point to files on our corporate intranet.
我想要一个基本 html 页面上的超链接列表,它指向我们公司内部网上的文件。
When a user clicks the link, I want the file to open. They are excel spreadsheets, and this is an intranet environment, so I can count on everyone having Excel installed.
当用户单击链接时,我希望打开文件。它们是 Excel 电子表格,而且这是一个 Intranet 环境,所以我可以指望每个人都安装了 Excel。
I've tried two things:
我尝试了两件事:
- The obvious and simple thing:
- 显而易见且简单的事情:
<a href="file://server/directory/file.xlsx">Click me!</a>
- A vbscriptoption that I found in a Google search:
- 我在谷歌搜索中找到的一个vbscript选项:
<HTML>
<HEAD>
<SCRIPT LANGUAGE=VBScript>
Dim objExcel
Sub Btn1_onclick()
call OpenWorkbook("\server\directory\file.xlsx")
End Sub
Sub OpenWorkbook(strLocation)
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = true
objExcel.Workbooks.Open strLocation
objExcel.UserControl = true
End Sub
</SCRIPT>
<TITLE>Launch Excel</Title>
</HEAD>
<BODY>
<INPUT TYPE=BUTTON NAME=Btn1 VALUE="Open Excel File">
</BODY>
</HTML>
I know this is a very basic question, but I would appreciate any help I can get.
我知道这是一个非常基本的问题,但如果我能得到任何帮助,我将不胜感激。
Edit: Any suggestions that work in both IE and Firefox?
编辑:任何在 IE 和 Firefox 中都适用的建议?
采纳答案by David Heggie
Try formatting the link like this (looks hellish, but it works in Firefox 3 under Vista for me) :
尝试像这样格式化链接(看起来很糟糕,但对我来说它适用于 Vista 下的 Firefox 3):
<a href="file://///SERVER/directory/file.ext">file.ext</a>
回答by diciu
<a href="file://server/directory/file.xlsx" target="_blank">
if I remember correctly.
<a href="file://server/directory/file.xlsx" target="_blank">
如果我没记错的话。
回答by Turnkey
If the file share is not open to everybody you will need to serve it up in the background from the file system via the web server.
如果文件共享不是对所有人开放,您将需要通过 Web 服务器从文件系统在后台提供它。
You can use something like this "ASP.Net Serve File For Download" example(archived copy of 2).
您可以使用类似“用于下载的 ASP.Net 服务文件”示例(2 的存档副本)。
回答by Sam Reynolds
You may need an extra "/"
您可能需要一个额外的“/”
<a href="file:///server/directory/file.xlsx">Click me!</a>
回答by Ken Ray
If your web server is IIS, you need to make sure that the new Office 2007 (I see the xlsx suffix) mime types are added to the list of mime types in IIS, otherwise it will refuse to serve the unknown file type.
如果你的web服务器是IIS,你需要确保在IIS中的mime类型列表中添加了新的Office 2007(我看到了xlsx后缀)的mime类型,否则会拒绝为未知的文件类型提供服务。
Here's one link to tell you how:
这里有一个链接告诉你如何:
回答by Orion Adrian
You're going to have to rely on each individual's machine having the correct file associations. If you try and open the application from JavaScript/VBScript in a web page, the spawned application is either going to itself be sandboxed (meaning decreased permissions) or there are going to be lots of security prompts.
您将不得不依赖于每个人的机器都具有正确的文件关联。如果您尝试在网页中从 JavaScript/VBScript 打开应用程序,则生成的应用程序本身要么会被沙盒化(意味着权限降低),要么会出现大量安全提示。
My suggestion is to look to SharePoint server for this one. This is something that we know they do and you can edit in place, but the question becomes how they manage to pull that off. My guess is direct integration with Office. Either way, this isn't something that the Internet is designed to do, because I'm assuming you want them to edit the original document and not simply create their own copy (which is what the default behavior of file://
would be.
我的建议是在 SharePoint 服务器上寻找这一点。这是我们知道他们所做的事情,您可以就地编辑,但问题在于他们如何设法实现这一目标。我的猜测是与 Office 直接集成。无论哪种方式,这都不是 Internet 旨在做的事情,因为我假设您希望他们编辑原始文档而不是简单地创建自己的副本(这是默认行为file://
。
So depending on you options, it might be possible to create a client side application that gets installed on all your client machines and then responds to a particular file handler that says go open this application on the file server. Then it wouldn't really matter who was doing it since all browsers would simply hand off the request to you. You would have to create your own handler like fileserver://
.
因此,根据您的选择,可能会创建一个客户端应用程序,该应用程序安装在您的所有客户端计算机上,然后响应特定的文件处理程序,该文件处理程序表示在文件服务器上打开此应用程序。那么谁在做这件事并不重要,因为所有浏览器都会简单地将请求交给您。您必须创建自己的处理程序,例如fileserver://
.
回答by Jan Han?i?
A simple link to the file is the obvious solution here. You just have to make shure that the link is valid and that it really points to a file ...
一个简单的文件链接是这里显而易见的解决方案。你只需要确保链接有效并且它确实指向一个文件......
回答by typemismatch
Your first idea used to be the way but I've also noticed issues doing this using Firefox, try a straight http:// to the file - href='http://server/directory/file.xlsx'
您的第一个想法曾经是这样,但我也注意到使用 Firefox 执行此操作的问题,尝试直接 http:// 到文件 - href=' http://server/directory/file.xlsx'