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

I am using pdf.js library in my application.It has integrated really well except for when i am trying

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745478419a4629452.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信