最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - pdf.js downloading as document.pdf instead of filename - Stack Overflow

programmeradmin1浏览0评论

I am using pdf.js library in my application. It has integrated really well except for when i am trying to download the document. Everytime i download a specific file it gets downloaded as document.pdf

I have quite a lot of files to download and this is creating a bit of confusion.

My code goes as below:

<iframe src="pdf_viewer/web/viewer.html?file=/docs/resumes/1b763820-e262-4f76-8502-8872a3cb52e8&filename=sample.pdf"></iframe>

my first parameter is the file id and the second parameter is the name with which the document should be downloaded as.

Below code is the one present in the pdf viewer viewer.js file

function getPDFFileNameFromURL(url) {
  var defaultFilename = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'document.pdf';
  console.log(url);
  console.log(defaultFilename);

  if (isDataSchema(url)) {
    console.warn('getPDFFileNameFromURL: ' + 'ignoring "data:" URL for performance reasons.');
    return defaultFilename;
  }
  var reURI = /^(?:(?:[^:]+:)?\/\/[^\/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/;
  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) {
      try {
        suggestedFilename = reFilename.exec(decodeURIComponent(suggestedFilename))[0];
      } catch (ex) {}
    }
  }
  return suggestedFilename || defaultFilename;
}

From my understanding of the code, what i am doing regarding the input is right. Where could i be going wrong ?

I am using pdf.js library in my application. It has integrated really well except for when i am trying to download the document. Everytime i download a specific file it gets downloaded as document.pdf

I have quite a lot of files to download and this is creating a bit of confusion.

My code goes as below:

<iframe src="pdf_viewer/web/viewer.html?file=/docs/resumes/1b763820-e262-4f76-8502-8872a3cb52e8&filename=sample.pdf"></iframe>

my first parameter is the file id and the second parameter is the name with which the document should be downloaded as.

Below code is the one present in the pdf viewer viewer.js file

function getPDFFileNameFromURL(url) {
  var defaultFilename = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'document.pdf';
  console.log(url);
  console.log(defaultFilename);

  if (isDataSchema(url)) {
    console.warn('getPDFFileNameFromURL: ' + 'ignoring "data:" URL for performance reasons.');
    return defaultFilename;
  }
  var reURI = /^(?:(?:[^:]+:)?\/\/[^\/]+)?([^?#]*)(\?[^#]*)?(#.*)?$/;
  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) {
      try {
        suggestedFilename = reFilename.exec(decodeURIComponent(suggestedFilename))[0];
      } catch (ex) {}
    }
  }
  return suggestedFilename || defaultFilename;
}

From my understanding of the code, what i am doing regarding the input is right. Where could i be going wrong ?

Share Improve this question edited Nov 3, 2017 at 14:39 Harsha Jasti asked Nov 3, 2017 at 14:32 Harsha JastiHarsha Jasti 1,2641 gold badge10 silver badges26 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

i figured out the solution

<iframe src="pdf_viewer/web/viewer.html?file=/docs/resumes/1b763820-e262-4f76-8502-8872a3cb52e8?sample.pdf"></iframe>

This takes the input in the url, now i can extract filename from the url

发布评论

评论列表(0)

  1. 暂无评论