在Django中,有什么方法可以在多个请求中访问上传的文件?

发布于 2025-02-10 00:58:21 字数 105 浏览 1 评论 0原文

我正在构建一个应用程序,用户可以上传他们的文件并转到付款页面。付款完成后,我想将用户上传的文件存储在数据库中。如果用户未完成付款,则我不想保存服务器或数据库上的文件。

有什么办法吗?

I am building an application where users can upload their files and go to the payment page. After payment completion, I want to store the files uploaded by the user in the database. If the user does not complete the payment, then I do not want to save the files on the server or database.

Is there any way to do that?

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

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

发布评论

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

评论(1

审判长 2025-02-17 00:58:21

在这里打开两件事。
首先,不建议将文件存储在数据库中。最好使用存储或直接使用服务器的文件系统。

其次,通常根据您的策略(在服务器的文件系统,数据库或第三方存储)上上传和保存文件,然后如果用户未进行付款,将进行清理。您需要定义进行清理的条件,无论是因为用户上传文件并且在一定时间段内一直不活动,还是因为他们单击了特定的按钮,还是两者的组合。

要触发清理,您有不同的可能性:

  • 上传文件时,安排任务,例如使用 django-q 要检查一下上传文件后1小时,如果付款尚未完成,则将文件删除
  • 编写一个django命令每天都会触发cron作业触发的cron作业,从比1小时,
  • 您还可以使用Django会议工作,并定期扫描1小时尚未活动的会话,他们的付款将待定,并假设这些用户将不会继续付款并删除其上传的文件

Two things to unpack here.
First, it is not advised to store files in the database. It's better to use a storage or directly the server's filesystem.

Second, files are usually uploaded and saved according to your strategy (on the server's filesystem, the database or a third party storage), and then a cleanup would happen if the user hasn't proceeded with the payment. You need to define the conditions for the cleanup to happen, whether it's because the user has uploaded file and has been inactive for a certain period of time, or because they click on a specific button, or a combination of both.

To trigger the cleanup, you have different possibilities:

  • When uploading the files, schedule a task, for instance using django-q to check that, say, 1 hour after uploading the files, if the payment hasn't been completed, the files are deleted
  • Write a django command that gets triggered by a cron job every day that deletes the files of the user whose payment has been pending for more than 1 hour
  • You could also work with django-sessions and regularly scan for sessions that haven't been active for 1 hour, whose payment are pending and assume these users will not proceed with the payment and delete their uploaded files
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文