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

File URL "Not allowed to load local resource" in the Internet Browser

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

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

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

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

这是我的代码行:

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/outcome 的 link

的屏幕截图

我没有意识到你原来的问题是你在本地机器上打开文件,我以为你是从网络服务器向客户端发送文件。

根据您的屏幕截图,尝试像这样格式化您的 link:

<a href="file:///C:/Projecten/Protocollen/346/Uitvoeringsoverzicht.xls">Klik hier</a>

(不知道你的每个记录集变量的内容我不能给你确切的 ASP 代码)

您必须为可通过浏览器访问的文件提供 link,例如:

<a href="http://my.domain.com/Projecten/Protocollen/346/Uitvoeringsoverzicht.xls">

对比

<a href="C:/Projecten/Protocollen/346/Uitvoeringsoverzicht.xls">

如果您将 "Projecten" 文件夹直接暴露给 public,那么您可能只需要提供 link 这样的:

<a href="/Projecten/Protocollen/346/Uitvoeringsoverzicht.xls">

但请注意,您的文件可能会被搜索引擎索引,任何拥有此文件的人都可以访问 link,等等

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

Not allowed to load local resource

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

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

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

在 Chrome (参见 )

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.

还有一个办法

由于您已经可以访问 Classic ASP,因此您始终可以构建一个为基于网络的文件提供服务的中间页面。您可以结合使用 ADODB.Stream 对象和 Response.BinaryWrite() 方法来执行此操作。这样做可确保您的网络文件位置永远不会暴露给客户端,并且由于脚本的灵活性,它可用于从多个位置和多种文件类型加载资源。

这是一个基本示例 ("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\Protocollen6\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> 中像这样用于 return 资源;

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

可以进一步采用这种方法并考虑 (尤其是对于较大的文件) 使用 Response.IsConnected 分块读取文件以检查客户端是否仍然存在 s.EOS 属性 在读取块时检查流的结尾。您还可以添加到 querystring 参数以设置是否要将文件 return in-line 或提示下载。


有用的链接

  • 使用 METADATA 导入 DLL 常量 - 如果您在识别 adTypeBinary 时遇到问题,那么硬编码 1.

    总是更好
  • Content-Disposition:What are the differences between “inline” and “attachment”? - 有关 Content-Disposition 如何在客户端上运行的有用信息。

对于不喜欢修改 chrome 的安全选项的人,我们可以简单地从包含您的本地文件的目录启动一个 python http 服务器:

python -m SimpleHTTPServer

和 python 3:

python3 -m http.server

现在您可以直接从您的 js 代码或外部使用 http://127.0.0.1:8000/some_file.txt

访问任何本地文件

您只需要将所有图像网络路径替换为 HTML 字符串中的字节字符串。首先,您需要 HtmlAgilityPack 将 Html 字符串转换为 Html 文档。 https://www.nuget.org/packages/HtmlAgilityPack

找到下面的代码将每个图像 src 网络路径(或本地路径)转换为字节串。 IE,chrome,firefox.

肯定会显示所有带有网络路径(或本地路径)的图片

编码的字符串HtmlString = Emailmodel.DtEmailFields.Rows[0]["Body"].ToString();

        // Decode the encoded string.
        StringWriter myWriter = new StringWriter();
        HttpUtility.HtmlDecode(encodedHtmlString, myWriter);
        string DecodedHtmlString = myWriter.ToString();

        //find and replace each img src with byte string
         HtmlDocument document = new HtmlDocument();
         document.LoadHtml(DecodedHtmlString);
         document.DocumentNode.Descendants("img")
          .Where(e =>
        {
            string src = e.GetAttributeValue("src", null) ?? "";
            return !string.IsNullOrEmpty(src);//&& src.StartsWith("data:image");
        })
        .ToList()
                    .ForEach(x =>
                    {
                        string currentSrcValue = x.GetAttributeValue("src", null);                                
                        string filePath = Path.GetDirectoryName(currentSrcValue) + "\";
                        string filename = Path.GetFileName(currentSrcValue);
                        string contenttype = "image/" + Path.GetExtension(filename).Replace(".", "");
                        FileStream fs = new FileStream(filePath + filename, FileMode.Open, FileAccess.Read);
                        BinaryReader br = new BinaryReader(fs);
                        Byte[] bytes = br.ReadBytes((Int32)fs.Length);
                        br.Close();
                        fs.Close();
                        x.SetAttributeValue("src", "data:" + contenttype + ";base64," + Convert.ToBase64String(bytes));                                
                    });

        string result = document.DocumentNode.OuterHtml;
        //Encode HTML string
        string myEncodedString = HttpUtility.HtmlEncode(result);

        Emailmodel.DtEmailFields.Rows[0]["Body"] = myEncodedString;

按照以下步骤操作,

  1. npm install -g http-server,在angular项目中安装http-server。
  2. 到需要访问的文件位置打开cmd提示符,使用cmd http 服务器 ./
  3. 在浏览器中访问任何带有端口号的路径(ex: 120.0.0.1:8080) 4.now 在您的 angular 应用程序中使用路径“http://120.0.0.1:8080/filename” 对我来说很好