Download pdf does not work - AngularJS, C #

0

I need to return a pdf and download it on the machine. But when you return the pdf, Blob does not recognize it.

$http.post($rootScope.raiz_ws + "/pdf/PdfCompraVenda", listProdutos)
            .then(function (response) {
                console.log(response);
                var file = new Blob([response], {type: 'application/pdf'});
                saveAs(blob, "RelatórioCompraVenda.pdf");
            });


[HttpPost]
        [Route("PdfCompraVenda")]
        public HttpResponseMessage Gerar(PdfLista listProdutos)
        {
        // criação do pdf no caminho
        // retorno abaixo
                    var path = @"C:\ERNetwork\ERNAdm\Relatorios\RelatórioCompraVenda.pdf";
                    HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
                    var stream = new FileStream(path, FileMode.Open);
                    result.Content = new StreamContent(stream);
                    result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
                    //result.Content.Headers.ContentDisposition.FileName = Path.GetFileName(path);
                    result.Content.Headers.ContentDisposition.FileName = Path.GetFileName(path);
                    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

                    return result;
            }
    
asked by anonymous 01.12.2017 / 21:02

1 answer

0

If someone needs to be able to adjust.

ANGULAR

$http.post($rootScope.raiz_ws + "/pdf/PdfCompraVenda", listProdutos)
            .then(function (response) {
                console.log(response.data);
                var file = new Blob([response.data], {type: 'application/pdf'});
                saveAs(file, "RelatórioCompraVenda.pdf");
            });

C #

[HttpPost]
[Route("PdfCompraVenda")]
public HttpResponseMessage Gerar(PdfLista listProdutos)
{
        // criação do pdf no caminho
        // retorno abaixo
        var path = @"C:\ERNetwork\ERNAdm\Relatorios\RelatórioCompraVenda.pdf";
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        var stream = new FileStream(path, FileMode.Open);
        result.Content = new StreamContent(stream);
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        result.Content.Headers.ContentDisposition.FileName = "RelatórioCompraVenda.pdf";
        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
        return result;

}
    
04.12.2017 / 14:20