I am trying to display a PDF after a successful AJAX call to my controller. I needed to modify the original way I was doing it to be able to handle multiple PDFs to return a single one.
The old way:
var uri = '../api/Controller/' + string1 + "," + string2 + "," + string3;
var src = '../web/printViewer.html?file=' + encodeURIComponent(uri);
window.open(src, "_blank");
So I have tried to keep the functionality of that with an AJAX call that looks like this:
$.ajax({
type: 'get',
url: '../api/Controller',
contentType: 'application/json; charset=utf-8',
data: { thing1: item, thing2: item2 },
datatype: 'json',
success: function (result) {
// no bueno D:
// but I get back a proper PDF every time
},
error: function () {
toastr.error('Error creating print.');
}
});
and for the C# controller:
[HttpGet]
public HttpResponseMessage Get(string thing1, string thing2) //thing2 will be a string[] eventually, once I am able to get 2-3 pdfs working
{
byte[] data = response.PDFBytes[0] == null ? new byte[0] : response.PDFBytes[0];
int length = response.PDFBytes[0] == null ? 0 : response.PDFBytes[0].Length;
var stream = new MemoryStream(data, 0, length, true, true);
result.Content = new ByteArrayContent(stream.GetBuffer());
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = "CheckedOutItems.pdf"
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
}
The first thing I tried to do was to just turn the entire thing into one big string, but it was getting far too long with only a few PDFs. I know the controller is returning a solid PDF, but I cannot seem to display it in the printViewer window.
Thanks in advance!!
EDIT:
I am not trying to download the file. I just wish to open it in a new window so the user can print it. Someone has suggested the same article twice now where the developer is trying to download the PDF. That is not what I am after.