Can I set the filename of a PDF object displayed in Chrome?

后端 未结 2 1242
醉话见心
醉话见心 2020-11-28 10:27

In my Vue app I receive a PDF as a blob, and want to display it using the browser\'s PDF viewer.

I convert it to a file, and generate an object url:

         


        
2条回答
  •  旧时难觅i
    2020-11-28 10:42

    Chrome's extension seems to rely on the resource name set in the URI, i.e the file.ext in protocol://domain/path/file.ext.

    So if your original URI contains that filename, the easiest might be to simply make your 's data to the URI you fetched the pdf from directly, instead of going the Blob's way.

    Now, there are cases it can't be done, and for these, there is a convoluted way, which might not work in future versions of Chrome, and probably not in other browsers, requiring to set up a Service Worker.

    As we first said, Chrome parses the URI in search of a filename, so what we have to do, is to have an URI, with this filename, pointing to our BlobURI.

    And to be able to do it, the only way I found for now, is to

    1. From the document, make a POST request, which will send the Blob to our ServiceWorker.
    2. From the ServiceWorker, cache the Blob that got sent
    3. Still from the ServiceWorker, return a new, fake URI, that we will map with the Blob cached in 2.
    4. From the document, set the src of an