简体   繁体   English

如何在 Heroku 上的 Flask 应用程序中使用 JS 下载文件的条件?

[英]How to make a condition to download a file using JS in a Flask app on Heroku?

I want to have a condition where when a user clicks a link to download a file it will ask for a password after which the file can be downloaded(if it is right).我想要一个条件,当用户单击链接下载文件时,它会要求输入密码,之后可以下载文件(如果正确的话)。 I am pretty basic in JS and I want to try this for my Flask app deployed at Heroku.我在 JS 方面非常基础,我想为部署在 Heroku 的 Flask 应用程序尝试这个。 I just don't know how to implement it.我只是不知道如何实现它。

So this is what I have come up with so far:所以这就是我到目前为止想出的:

<main class="page projects-page">
        <section class="portfolio-block projects-cards">
            <div class="container">
                <div class="heading">
                    <h2>Test download</h2>
                </div>
                <div class="row">
                    <div class="col-md-6 col-lg-4">
                        <div class="card border-0"></div>
                    </div>
                    <div class="col-md-6 col-lg-4">
                        <div class="card border-0"><a href="#"></a>
                            <div class="card-body">
                                <h6><a href="#">To download the file enter the SECRET_KEY</a></h6><input type="password"></div>
                        </div><button class="btn btn-primary d-block float-none" type="submit" style="width: 349.984px;">DOWNLOAD&nbsp;</button></div>
                    <div class="col-md-6 col-lg-4">
                        <div class="card border-0"></div>
                    </div>
                    <div class="col-md-6 col-lg-4">
                        <div class="card border-0"><a href="#"></a>
                            <div class="card-body"></div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
</main>

This is the part of the code where the user types the password and if correct the file should be downloaded.这是用户键入密码的代码部分,如果正确,则应下载文件。 Can this be done with a simple JS function where the password field and button are enclosed in a form element and compared or should I use another method?这可以通过简单的 JS function 来完成,其中密码字段和按钮包含在表单元素中并进行比较,还是应该使用其他方法?

So, when a link is clicked you can generate a modal asking for the password, when the password is entered and submitted, you send both the password and which link is being requested to the API and it initiates the download if the link is correct.因此,当单击链接时,您可以生成询问密码的模式,当输入并提交密码时,您将密码和请求的链接都发送到 API,如果链接正确,它会启动下载。

Here's a simple modal:这是一个简单的模态:

<div class="modal-container" id="modal">
<div class="modal">
<button class="close-btn" id="close"></button>
<div class="modal-header">
<h3>Password needed to access file </h3>
<div class="modal-content">
<form class="modal-form">
<div>
<label for="password">Password</label>
<input
type="password"
id="password"
placeholder="Enter Password"
class="form-input"
/>
</div>
<input type="submit" value="Submit" class="submit-btn" />
</form>
</div>
</div>
</div>
</div>

now you can add an event listener on the link items so that when the link is clicked, this modal appears and the user is prompted to enter a password.现在您可以在链接项目上添加事件侦听器,以便在单击链接时出现此模式并提示用户输入密码。 I handle the modal appearing and disappearing in css, you can see my css at我处理 css 中出现和消失的模态,您可以在以下位置查看我的 css

https://github.com/Daniel-Wh/BurgerAndModal https://github.com/Daniel-Wh/BurgerAndModal

I'm assuming that you already have experience with event listeners and making API calls since it sounds like someone can click a link and download something from your app.我假设您已经有使用事件侦听器和进行 API 调用的经验,因为听起来有人可以单击链接并从您的应用程序中下载某些内容。

Without seeing your code, there's not much else I can say.没有看到您的代码,我无话可说。 Good luck out there祝你好运

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

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