使用Azure应用程序服务下载pdf文件时出现406(不可接受)错误

时间:2018-01-15 17:08:04

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

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

我如何解决这个问题?仅使用localhost。

韩国社交协会

1 个答案:

答案 0 :(得分:1)

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