In my backend I have a function that returns a pdf
public HttpResponseMessage Get()
{
var dataSet = new Reports.DataSet1();
dataSet.dsestado.AdddsestadoRow("1", "parana","pr", "1");
dataSet.dsestado.AdddsestadoRow("2", "parana","pr", "1");
dataSet.dsestado.AdddsestadoRow("3", "parana", "pr", "1");
var report = new Microsoft.Reporting.WebForms.LocalReport();
report.ReportPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Reports/Report1.rdlc");
report.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("DataSet1", (System.Data.DataTable)dataSet.dsestado));
report.Refresh();
string mimeType = "";
string encoding = "";
string filenameExtension = "";
string[] streams = null;
Microsoft.Reporting.WebForms.Warning[] warnings = null;
byte[] bytes = report.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streams, out warnings);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
result.Content = new ByteArrayContent(bytes);
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(mimeType);
return result;
}
How can I improve the following method to save this pdf to disk, or open it in the browse?
$scope.relatorio = function () {
$http.post($scope.pathMyApp + "/RelEstado/Get")
.then(function (data) {
})
.catch(function (error) {
console.log(error);
var str = "ERRO INESPERADO";
$scope.sendMail("Pessoa - getAllPessoaGrid: " + error, "Erro front end zulex");
UIkit.modal.alert('<div class=\'uk-text-center\'>' + str + '<br/><img id=\'img-erro\' class=\'uk-margin-top\' style=\'width: 18%;margin-bottom: -60px;\' src=\'assets/img/spinners/erro.png\' </div>', { labels: { 'Ok': 'OK' } });
$scope.progressbar.complete();
});
$scope.content_preloader_hide();
};
I tried two ways:
<a href="http://localhost:53724/RelEstado/get" download="teste.pdf">Download it!</a>
and
$scope.relatorio2 = function () {
var fileURL = 'http://localhost:53724/RelEstado/get';
var fileName = 'teste.pdf';
// for non-IE
if (!window.ActiveXObject) {
var save = document.createElement('a');
save.href = fileURL;
save.target = '_blank';
save.download = fileName || 'unknown';
var evt = new MouseEvent('click', {
'view': window,
'bubbles': true,
'cancelable': false
});
save.dispatchEvent(evt);
(window.URL || window.webkitURL).revokeObjectURL(save.href);
}
// for IE < 11
else if (!!window.ActiveXObject && document.execCommand) {
var _window = window.open(fileURL, '_blank');
_window.document.close();
_window.document.execCommand('SaveAs', true, fileName || fileURL)
_window.close();
}
}
In both cases, the pdf is downloaded, but when trying to open the error "Failed to load PDF document.", I am in doubt if the backend function is correct.