I'm trying to download and save a PDF file using ajax/jquery (I know..).
This is what I have on the server side:
public HttpResponseMessage GetPdf()
{
var pdf = generatePdfByteArray(); // byte[]
var result = Request.CreateResponse(HttpStatusCode.OK);
result.Content = new ByteArrayContent(pdf);
//result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
//{
// FileName = "blah.pdf"
//};
// tried with and without content disposition.. shouldn't matter, i think?
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return result;
}
This is the client side:
let ajaxOptions = {
url: '/url',
type: "GET",
accepts: "application/pdf",
success: (data) => {
let blob = new Blob(data, {
type: "application/pdf"
}); // <-- this fails
// stuff...
}
};
$.ajax(ajaxOptions);
Any ideas what's wrong with this?
The first parameter should be sequence.
Thus, this will not work:
let blob = new Blob(data, {
type: "application/pdf"
});
But this will:
let blob = new Blob([data], {
type: "application/pdf"
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With