【发布时间】: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