Sinatra 在上传之前执行代码

发布于 2025-01-02 17:57:51 字数 602 浏览 1 评论 0原文

我的页面上有一个简单的 HTML 表单,如下所示:

<form action="/" method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="input" name="pin" />
    <input type="submit" value="Upload" />
</form>

现在我想要完成的任务(使用 Sinatra)是检查表单字段中输入的 PIN 是否正确:

post "/" do
    if params[:pin] == "1234"
        start_upload()
    else
        print_error_message()
    end
end

当然,我希望 PIN 为在文件开始上传之前检查。但这是我的问题。单击“上传”按钮后,文件立即开始上传,直至完成。然后脚本检查 PIN 是否有效。

有没有办法在文件上传开始之前做一些事情?如果没有,还有哪些其他方法可以做到这一点?

I'm having a simple HTML form on my page that looks like this:

<form action="/" method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="input" name="pin" />
    <input type="submit" value="Upload" />
</form>

Now what I am trying to accomplish (with Sinatra) is to check if the PIN entered into the form field is correct:

post "/" do
    if params[:pin] == "1234"
        start_upload()
    else
        print_error_message()
    end
end

Of course, I want the PIN to be checked before the file starts uploading. But that's my problem. Immediately after clicking the "Upload" button, the file upload starts until it is finished. Then the script checks to see if the PIN is valid.

Is there a way to do stuff before the file upload starts? And if not, what other ways of doing this are there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸里的快感 2025-01-09 17:57:51

除非您使用一些 Ajax 并拆分您的请求,否则这是行不通的。您可以有两种表单,一种用于保存 PIN 码,另一种用于授权用户。输入正确的 PIN 码后,您将向服务器发送异步请求,服务器将回复肯定或否定的答案。根据响应,某些 JavaScript 将启用您的文件上传按钮,以便您可以开始上传文件。您还应该做的是为用户设置一个会话,以便仅允许授权用户(通过 PIN 码)发送表单。如果您查看 Sinatra 自述文件,您可以找到一些有关如何执行此操作的信息。

这就是我的解决方案。

Unless you use some Ajax and split up your request this won't work. You could have two forms, one that holds the pin and that authorizes the user. Once you enter a correct pin you send an asyn request to the server which will then reply with a positive or a negative answer. Depending on the response some javascript will then enable your file upload button so you can start uploading the file. What you should also do is setting a session for the user so that only an authorized user (via the pin) is allowed to send a form. If you check the Sinatra Readme you can find some information on how to do that.

That would be my solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文