简体   繁体   English

JavaScript 如果提示密码为真则下载文件

[英]JavaScript download a file if prompt password is true

I want to download a file if user enters the correct password如果用户输入正确的密码,我想下载文件

I have a button in my html file:我的 html 文件中有一个按钮:

<button onclick="password()"><h3>Code</h3> </a>

in my.js file, here is the function:在 my.js 文件中,这里是 function:

function password() {
    var password = prompt("Please enter the password");
    if (password === "password") {
        //download the file
    }
    else {
        alert("Password incorrect");
    }
}

the file to be downloaded is located at "../file/file.txt"要下载的文件位于“../file/file.txt”

I am unsure how to download the file once user enters the correct file一旦用户输入正确的文件,我不确定如何下载文件

Try this.尝试这个。

<a id="download" style="display: none" 
    href="../file/file.text" 
    download>Download</a>


function password() {
    var password = prompt("Please enter the password");
    if (password === "password") {
        document.getElementById('download').click();
    }
    else {
        alert("Password incorrect");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM