【问题标题】:Angular - Unable to download a zip fileAngular - 无法下载 zip 文件
【发布时间】:2020-02-07 17:39:02
【问题描述】:

我有一个 Angular 应用程序,它按预期工作,除了能够从网络路径下载 zip 文件。

我收到以下错误消息: “从源 'http://localhost:4200' 访问位于 'file://IPAddress/d$/***/Output.zip' 的 XMLHttpRequest 已被 CORS 策略阻止: 跨域请求仅支持协议方案:http、data、chrome、chrome-extension、https。"

这是我用来下载 zip 文件的函数:

downloadFile(filePath: string) {
    return this.http.get(filePath, {
      responseType: 'arraybuffer'
    }).subscribe(data => {
      const blob = new Blob([data], {
        type: 'application/zip'
      });
      const url = window.URL.createObjectURL(blob);
      window.open(url);
    });
  }

重要的是要指出,通过在 IIS 8.5 上托管 Angular 应用程序,我也面临同样的问题。这是 IIS 8.5 中属于 Angular 应用程序的 web.config 文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Angular Routes" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/ApplicationName/"/>
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

您能帮忙解决这个问题吗?

【问题讨论】:

    标签: angular iis


    【解决方案1】:

    这看起来像是一个 CORS 问题。

    IIS需要发送HeaderAccess-Control-Allow-Origin: *

    也许这有助于在 IIS 中启用它: https://enable-cors.org/server_iis7.html

    【讨论】:

    • 嗨。感谢您的回复,但这并没有解决问题。
    • 您好,我修改了问题的描述。你能看一下吗?
    猜你喜欢
    • 1970-01-01
    • 2017-10-22
    • 2018-02-09
    • 1970-01-01
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    相关资源
    最近更新 更多