无法在客户端验证密码

我正在尝试在我的网页上使用,pdf.js在将其上传到服务器之前,我需要验证 PDF 用户输入的密码是否正确。


但我无法让它发挥作用。


我使用 CDN 托管pdf.min.js


<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.min.js" integrity="sha512-Z8CqofpIcnJN80feS2uccz+pXWgZzeKxDsDNMD/dJ6997/LSRY+W4NmEt9acwR+Gt9OHN0kkI1CTianCwoqcjQ==" crossorigin="anonymous"></script>

这是密码检查功能:


function checkPass() {

pdfjsLib.getDocument({

        url: 'pdf.pdf',

        password: '01010101'

    }).promise.then(function (pdf) {

        input.classList.add("rightpass");

    }).catch(function (error) {

        input.classList.add("wrongpass");

        console.log(error);

    });

在控制台日志中,它记录Deprecated API usage: No "GlobalWorkerOptions.workerSrc" specified.


我试图禁用它,pdfjsLib.disableWorker = true;但没有用。


我只需要验证密码,没有别的没有呈现。


小唯快跑啊
浏览 497回答 1
1回答

慕丝7291255

这是一个工作代码示例:<html><head><script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.6.347/pdf.worker.min.js"></script><script>&nbsp; &nbsp; const pdfName = "empty-protected.pdf";&nbsp; &nbsp; async function loaded()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; const loadingTask = pdfjsLib.getDocument({ url: pdfName, password: "qwerty" });&nbsp; &nbsp; &nbsp; &nbsp; loadingTask.promise.then(() => console.log(1, "OK"), (ex) => console.log(1, ex));&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; const loadingTask2 = pdfjsLib.getDocument({ url: pdfName, password: "" });&nbsp; &nbsp; &nbsp; &nbsp; loadingTask2.promise.then(() => console.log(2, "OK"), (ex) => console.log(2, ex));&nbsp; &nbsp; }</script></head><body onload="javascript:loaded();"></body></html>qwerty这是我用于实验的受密码保护的 pdf 文件(密码是):https://mega.nz/file/lJg2GKgS#AdVJ0DZ1OfsGrlSqM67dfr_r2n8lBiiOm9q0o4wt7e4包含pdf.worker.min.js将警告更改"Warning: Deprecated API usage: No "GlobalWorkerOptions.workerSrc" specified."为"Warning: Setting up fake worker."
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript