Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Failed to construct 'Blob': The provided value cannot be converted to a sequence" when downloading file

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?

like image 378
Thinkhoop Avatar asked Mar 12 '19 21:03

Thinkhoop


1 Answers

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"
});

like image 149
Ahti Ahde Avatar answered Nov 06 '22 16:11

Ahti Ahde