Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
96
rated 0 times [  97] [ 1]  / answers: 1 / hits: 28714  / 10 Years ago, sat, february 22, 2014, 12:00:00

On my server side I am using ASP.NET MVC Web Api, where I am generating the PDF file with Crystal report and exporting it to PDF format. The code goes as follows:



[HttpPost]
public HttpResponseMessage SetReport(string name, [FromBody]List<KontoDto> konta)
{
var response = Request.CreateResponse(HttpStatusCode.OK);
var strReportName = KontoReport.rpt;
var rd = new ReportDocument();
string strPath = HttpContext.Current.Server.MapPath(~/) + Reports// + strReportName;
rd.Load(strPath);
rd.SetDataSource(konta);
var tip = ExportFormatType.PortableDocFormat;
var pdf = rd.ExportToStream(tip);
response.Headers.Clear();
response.Content = new StreamContent(pdf);
response.Content.Headers.ContentType = new MediaTypeHeaderValue(application/pdf);
return response;

}


My Javascript code is:



  $scope.xxprint = function () {
console.log($scope.konta);
$http.post('/api/konto/setReport/pdf', $scope.konta, { responseType: 'arraybuffer' })
.success(function (data) {
var file = new Blob([data], { type: 'application/pdf' });
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
});
};


This simply does not work. I don't know what's wrong with this code. I get the browser to open the pdf viewer, but it is empty. Also, the created pdf is correctly created as I can save it to disk and open it then with Adobe Acrobat viewer. The content of the HttpResponseMessage seems also correct viewed via Fiddler. See image:



enter


More From » asp.net

 Answers
43

Seems I did it correctly all the time. The problem was with my angularjs version (v1.08). When upgrading to v1.2 everything worked ok. In v1.08 the responseType: 'arraybuffer' parameter (which is crucial to what I was doing) was simply ignored by angularjs. It seems to be implemented as of v.1.1. See this SO question: How to read binary data in AngularJS in an ArrayBuffer?


[#72377] Thursday, February 20, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jazminkyrap

Total Points: 631
Total Questions: 89
Total Answers: 109

Location: Finland
Member since Fri, Oct 21, 2022
2 Years ago
;