【问题标题】:406(Not Acceptable) error when downloading pdf files with Azure app services使用 Azure 应用服务下载 pdf 文件时出现 406(不可接受)错误
【发布时间】:2018-06-24 08:39:51
【问题描述】:

WebAPI:.NetCore 2.0; 前台/APP:Angular 4;

我的 webapi 中有一个导出为 pdf 文件的方法,该方法在我的机器上运行,但在 Azure 上发布后不起作用。这是控制器上的方法:

 [HttpGet("DownloadPDF")]
    public IActionResult DownloadPropostaPDF(int propostaId)
    {
        var telemetry = new TelemetryClient();
        try
        {
            var nomeProposta = _propostasAppService.CriarProposta(propostaId, "Juntos");
            var proposta = _propostasAppService.BuscarPorId(propostaId);
            if (nomeProposta != null)
            {
                string contentType = "application/pdf";
                HttpContext.Response.ContentType = contentType;
                var pdfPath = Path.Combine(Directory.GetCurrentDirectory(), "Arquivos", proposta.Numero, nomeProposta.Replace(".docx", ".pdf"));
                var docxPath = Path.Combine(Directory.GetCurrentDirectory(), "Arquivos", proposta.Numero, nomeProposta);
                if (_propostasAppService.GerarPdf(docxPath))
                {
                    var file = System.IO.File.ReadAllBytes(docxPath);
                    var result = new FileContentResult(file, contentType)
                    {
                        FileDownloadName = nomeProposta.Replace(".docx", ".pdf")
                    };
                    return result;
                }
                else
                {
                    Response.StatusCode = 404;
                    return null;
                }
            }
            Response.StatusCode = 404;
            return null;
        }
        catch (Exception ex)
        {
            telemetry.TrackException(ex);
            throw ex;
        }
    }

应用组件代码:

downloadPropostaPDF(idProposta) {
this.propostasService.getProposta(idProposta).subscribe(proposta => {
  var nomeArquivo = proposta.formatoPropostaNome + " Blend IT " + proposta.numero
    + " - " + proposta.titulo + " - " + proposta.clienteNomeFantasia
    + " - v" + proposta.versao + ".pdf"
  this.propostasService.downloadPdf(idProposta, "Juntos").subscribe(data => {
    const blob = new Blob([data], { type: 'application/pdf' })
    saveAs(blob, nomeArquivo);
  },
    error => {
      console.log(error)
    }
  )
})

}

应用服务:

downloadPdf(propostaId: number, formatoDocumento: any): any {
    const _url = Configuration.apiUrl + 'api/propostas/DownloadPdf/' + '?propostaId=' + propostaId
    return this.http.get(_url, { headers: this.headersBlobPdf, responseType: 'blob' })
}

Headers:
    Access-Control-Request-Method:GET
    Origin:http://propostas-homolog.azurewebsites.net
    X-DevTools-Emulate-Network-Conditions-Client-Id:(50FE08B74C0BC8F9BE1441A68EB6B34)
    User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
    Access-Control-Request-Headers:authorization
    Accept:*/*
    Accept-Encoding:gzip, deflate
    Accept-Language:pt-BR,pt;q=0.9

我该如何解决这个问题?仅适用于本地主机。

Tks

【问题讨论】:

  • 当你说“不工作”时,它在做什么呢?除了响应码之外还有错误信息吗?
  • 只是http错误响应。

标签: angular azure asp.net-web-api .net-core azure-web-app-service


【解决方案1】:

似乎发生了一些底层异常,导致应用程序返回错误代码。尝试通过转到 Azure 门户然后选择“诊断和解决问题”并在其下转到“诊断即服务”来获取 CLR 分析器跟踪。在那里,确保选择了 CLR 分析器诊断器并点击运行按钮.启动探查器跟踪后,尝试通过向您的 Web API 发出一些请求来重现您的问题。分析器将在 60 秒后自动停止并生成报告。该报告有一个失败的请求部分,它将向您显示所有失败的请求和相关的异常,如果它找到一个......希望这有助于缩小问题的范围。

【讨论】:

    猜你喜欢
    • 2015-10-15
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 2012-11-11
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    相关资源
    最近更新 更多