Opening PDF from POST in a new tab

What do I need to do:

I need to open the file on the new tab in Firefox (other browser support is not important)

This file_name must match Content-Disposition.

So I send the data in my POST to the server, and then I get the PDF file in response. Response headers (example):

Content-Type: application/pdf 
Access-Control-Expose-Headers: Content-Disposition
Content-Disposition: attachment; filename="file name.pdf" 
Content-Length: 234

My answer is the PDF file itself (Blob -> application / pdf)

It is also possible to have multiple requests at once

What is done:

Now the file opens in a new window, but with the wrong file name (the file name is not used). It is also blocked by Firefox.

My code (not all, only the important part):

var xhr = new XMLHttpRequest();
                xhr.onreadystatechange = function(){
                    if (this.readyState == 4 && this.status == 201){
                        var filename = this.getResponseHeader('Content-Disposition').split('=')[1];
                        filename = filename.substr(1, filename.length-2);

                        var url = window.URL.createObjectURL(this.response);
                        window.open(url);
                    }
                };
                xhr.open('POST', host + this.getLink('preview', this.collection.jsonSchemaSwapData).href);

                xhr.setRequestHeader("Content-type","application/json");
                xhr.responseType = 'blob';
                xhr.send(JSON.stringify(this.toJSON()));

Problems:

  • (, )
  • CreateObjectURL
  • pdf ,

PDF ?

+4

Source: https://habr.com/ru/post/1599151/


All Articles