html - Internet 浏览器中的文件 URL "Not allowed to load local resource"

标签 html url asp-classic href

我有一个重要的脑筋急转弯。

我想在经典 ASP 中打开一个文件。我正在使用各种变量,因为事情可能会发生变化,但结果是正确的。我知道这一点,因为我已经通过复制链接地址并将其放入我的 URL 来测试结果。现在的问题是:如果我单击我的链接,它不会执行任何操作。不是刷新,不是重定向。没有什么。有谁知道我做错了什么?

好的,这就是交易。我的文件并不总是本地的,这取决于我所处的环境。如果我复制粘贴我的网址的结果,它会下载。如果我单击我的 URL,它不会响应。有任何想法吗?浏览器问题? (虽然我已经测试了 5 个浏览器)或者其他什么?我真的被困在这里,互联网似乎并不站在我这边。

我有 3 个环境。此处下面的变量是为了使链接有效。我知道该链接有效,因为我已经通过复制对其进行了测试。是的,它确实以 file:/// 开头,是的,我确定链接是正确的。

这是我的代码行:

response.write("<td class='tab_kolom2'><a href='"&rootRs("pre_rootpad")&rootRs("rootpad_protocollen")&"\"&overzichtRs("Formuliernr")&"\Uitvoeringsoverzicht.xls' target='_blank' download>Click here</a></td>")

编辑:带有错误/链接结果的屏幕截图

error

最佳答案

现在我们知道实际的错误是什么可以制定一个答案。

Not allowed to load local resource

是 Chrome 和其他现代浏览器中内置的安全异常(exception)。措辞可能不同,但在某种方式或形式上,它们都有适当的安全异常(exception)来处理这种情况。

在过去,您可以覆盖某些设置或应用某些标志,例如

--disable-web-security --allow-file-access-from-files --allow-file-access

in Chrome (See https://stackoverflow.com/a/22027002/692942)

It's there for a reason

At this point though it's worth pointing out that these security exceptions exist for good reason and trying to circumvent them isn't the best idea.

There is another way

As you have access to Classic ASP already you could always build a intermediary page that serves the network based files. You do this using a combination of the ADODB.Stream object and the Response.BinaryWrite() method. Doing this ensures your network file locations are never exposed to the client and due to the flexibility of the script it can be used to load resources from multiple locations and multiple file types.

Here is a basic example ("getfile.asp"):

<%
Option Explicit

Dim s, id, bin, file, filename, mime

id = Request.QueryString("id")

'id can be anything just use it as a key to identify the 
'file to return. It could be a simple Case statement like this
'or even pulled from a database.
Select Case id
Case "TESTFILE1"
  'The file, mime and filename can be built-up anyway they don't 
  'have to be hard coded.
  file = "\\server\share\Projecten\Protocollen\346\Uitvoeringsoverzicht.xls"     
  mime = "application/vnd.ms-excel"
  'Filename you want to display when downloading the resource.
  filename = "Uitvoeringsoverzicht.xls"

'Assuming other files 
Case ...
End Select

If Len(file & "") > 0 Then
  Set s = Server.CreateObject("ADODB.Stream")
  s.Type = adTypeBinary 'adTypeBinary = 1 See "Useful Links"
  Call s.Open()
  Call s.LoadFromFile(file)
  bin = s.Read()

  'Clean-up the stream and free memory
  Call s.Close()
  Set s = Nothing

  'Set content type header based on mime variable
  Response.ContentType = mime
  'Control how the content is returned using the 
  'Content-Disposition HTTP Header. Using "attachment" forces the resource
  'to prompt the client to download while "inline" allows the resource to
  'download and display in the client (useful for returning images
  'as the "src" of a <img> tag).
  Call Response.AddHeader("Content-Disposition", "attachment;filename=" & filename)
  Call Response.BinaryWrite(bin)
Else
  'Return a 404 if there's no file.
  Response.Status = "404 Not Found"
End If
%>

此示例是伪编码的,因此未经测试。

然后可以在 <a> 中使用此脚本像这样返回资源;

<a href="/getfile.asp?id=TESTFILE1">Click Here</a>

可以进一步采用这种方法并考虑(尤其是对于较大的文件)使用Response.IsConnected 以 block 的形式读取文件检查客户端是否还在那里 s.EOS属性以在读取 block 时检查流的结尾。您还可以添加到查询字符串参数以设置您是希望文件以内联方式返回还是提示下载。


有用的链接

关于html - Internet 浏览器中的文件 URL "Not allowed to load local resource",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34901523/

相关文章:

javascript - jQuery如何抓取元素?

php - url 重写并传递表单的值

angular - 如何在 Angular 5 中删除 url 中的#?

asp.net - 与 ASP.net 相比,经典 ASP 有何优势

javascript - 使用 javascript 使图片在按钮单击时发生变化

html - Chrome 对齐元素 : baseline for select and input elements

css - 如何使用 HTML-CSS 将倾斜/倾斜的背景颜色设置为文本

Python 从列表中过滤/删除 URL

asp-classic - 无法创建ActiveX对象

sql - 使用 ADODB.Command.Execute 设置 CursorType