Download pdf not working - Angularjs, C#

Asked

Viewed 113 times

0

I need to return a pdf and download in the makeup of it. However when returning 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;
            }

1 answer

0

If anyone needs 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;

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.