How to change filename for download?

Asked

Viewed 1,262 times

6

I am generating a PDF file and use HTML5 to display it on the screen:

 $("#conteudo-pdf").append('<object data="' + meuData + '" type="application/pdf" style="width: 100%; height: 100%"></object>')

The content is displayed correctly, and a toolbar is created, where it is possible to download the file:

Firefox:
inserir a descrição da imagem aqui

Chrome:
inserir a descrição da imagem aqui

IE:
inserir a descrição da imagem aqui

Opera:
inserir a descrição da imagem aqui

By clicking "Download" the following screen opens;

inserir a descrição da imagem aqui


Currently, for each browser, a different default file name is generated. In this case, Firefox brings predefined as "Document.pdf".

I would like to change it to a name of my preference that works on all browsers.

That’s possible?

  • Excellent question. I can’t imagine how to do that!

  • This might help, http://stackoverflow.com/questions/7717851/save-file-javascript-with-file-name

  • @David, in my case the problem is that the toolbar is created dynamically via HTML 5, and each Browser behaves in a different way, making it impossible to use hint given in the link you posted.

  • @Sanction is a link.

1 answer

5

If it is possible to edit the toolbar test this:

<a href="download/document.pdf" download="nome_desejado">Download PDF</a>

Based on your comment I decided to edit the reply!

After analyzing an open pdf in Firefox, I noticed that it loaded a js called "Viewer.js" and it looks interesting function inside it:

function getPDFFileNameFromURL(url) {
  var reURI = /^(?:([^:]+:)?\/\/[^\/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/;
  //            SCHEME      HOST         1.PATH  2.QUERY   3.REF
  // Pattern to get last matching NAME.pdf
  var reFilename = /[^\/?#=]+\.pdf\b(?!.*\.pdf\b)/i;
  var splitURI = reURI.exec(url);
  var suggestedFilename = reFilename.exec(splitURI[1]) ||
                           reFilename.exec(splitURI[2]) ||
                           reFilename.exec(splitURI[3]);
  if (suggestedFilename) {
    suggestedFilename = suggestedFilename[0];
    if (suggestedFilename.indexOf('%') !== -1) {
      // URL-encoded %2Fpath%2Fto%2Ffile.pdf should be file.pdf
      try {
        suggestedFilename =
          reFilename.exec(decodeURIComponent(suggestedFilename))[0];
      } catch(e) { // Possible (extremely rare) errors:
        // URIError "Malformed URI", e.g. for "%AA.pdf"
        // TypeError "null has no properties", e.g. for "%2F.pdf"
      }
    }
  }
  return suggestedFilename || 'document.pdf';
}

Ready, now you know how it brings the default name "Document.pdf".

  • It is not possible to edit the toolbar as it is created dynamically via HTML 5, and each browser creates in a different way. In addition, in the case of Firefox, for example, a button and not a link. Yet I tested the attribute download, but it didn’t work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.