如何处理上传文件的临时存储

发布于 2024-09-18 07:34:29 字数 277 浏览 3 评论 0 原文

在我的 Django 应用程序中,我有一个多步骤注册,几乎没有条件参数。因此,我想出了在会话中存储表单数据的方法。不幸的是,会话使用pickle序列化数据,它不支持文件序列化并导致
PicklingError:无法pickle :属性查找cStringIO.StringO失败。如何解决这个问题?我应该将图像作为变量发送到所有以下视图,还是将其作为 GET 参数发送或以其他方式发送?我不确定是否需要任何示例代码,因为问题似乎很清楚。

In my django application I have a multi step registration, with few conditional parameters. Because of this I figured out to store data from forms in session. Unfortunatelly sessions serialize data using pickle, which doesn't support file serialization and causes
PicklingError: Can't pickle <type 'cStringIO.StringO'>: attribute lookup cStringIO.StringO failed . How to get around this problem ? Should I send image as a variable to all following views, or send it as a GET parameter or do it in some other way ? I'm not sure if any sample code is needed since problems seems pretty clear.

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

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

发布评论

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

评论(1

站稳脚跟 2024-09-25 07:34:29

如果正在上传的文件大小超过几 KB,那么您可能不希望将它们存储在会话中(并且您绝对不希望通过 GET 将它们发送回浏览器)。

我可以想到几个选择:

  • 您可以重写注册表单,以便上传的文件放在最后。
  • 您可以使表单成为单个步骤,并使用 javascript 伪造多步骤(例如,通过隐藏和显示 DIV)。
  • 您可以将临时文件保留在磁盘上,并将文件名存储在会话中(记住定期清理旧文件)
  • 您可以简化注册,并在“个人资料”页面上上传文件(也许强制执行“您有在允许访问网站的其余部分之前填写个人资料”要求)。

If the files that are being uploaded are larger than a few KB in size, then you probably don't want to store them in the session (and you definitely don't want to send them back to the browser via a GET).

I can think of a few options:

  • You could rewrite your registration form so that uploaded files come last.
  • You can make your form a single step, and fake the multi-step with javascript (by hiding and showing DIV's, for example).
  • You could keep the temporary files on disk, and store the file names in the session (remembering to clean up old files periodically)
  • You could simplify your registration, and do the file upload on a "profile" page (perhaps enforcing a "you have to fill out the profile" requirement before allowing access to the rest of the site).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文