带表单验证的跨站点文件上传

发布于 2024-10-03 00:08:54 字数 197 浏览 3 评论 0原文

我想从 iframe 中的某种形式将文件上传到远程服务器。但在提交表单之前,我想使用 Django 的内置系统检查它是否有效。首先如何在不重定向的情况下将文件上传到远程服务器。我只需要指定空 iframe 作为表单的目标吗?其次,如何环绕提交,我不知道,也许是一些 ajaxSubmit 事件,来评估表单,并且仅当它有效时,然后执行提交本身(所以函数可能应该返回 True 等)?

I would like to upload a file to remote server from some form in an iframe. But before submitting the form I'd like to check if it's valid using Django's built-in systems. So first how to upload file to a remote server without redirecting. Do I only need to specify empty iframe as form's target? And secondly how to wrap around submition I don't know, maybe some ajaxSubmit event, to evaluate the form and only when it's valid and then perform the submit itself (so probably the function should return True etc)?

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

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

发布评论

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

评论(1

孤独难免 2024-10-10 00:08:54

也许我没有正确理解你的问题,但听起来你想对表单中输入的数据使用 Django 基于服务器的表单验证。执行此操作的唯一方法是将表单实际提交到 Django 服务器。

通常,将文件上传到“远程”服务器的方法是提交表单并让该远程服务器处理表单提交(包括文件内容)。如果远程服务器也是基于 django 的,您可以使用相同的验证(假设您没有使用 CSRF 即——我不确定是否可以使用 CSRF 指向另一台服务器,主要是因为这是 CSRF 全部要点的一部分)。

那么,你真正的目标是什么?是否有第二台服务器(不是 django 服务器)需要该文件?如果是这样,我将在 Django 服务器上处理上传并执行验证,然后使用 ftp、sftp、scp 等连接将文件复制到其他服务器。

或者,您可以编写自己的 JavaScript(或现有的 JS 框架)来验证表单,然后将表单提交到远程服务器。

顺便说一句,无需使用 iframe(无论是否为空)。仅当您的目标是在不离开页面的情况下异步提交文件时才需要这样做。

Maybe I'm not understanding your question correctly, but it sounds like you want to use Django's server-based form validation on the data entered in the form. The only way to do this is to actually submit the form to the Django server.

Normally, the way to upload a file to a "remote" server is by submitting a form and having that remote server process the form submission (including the file content). If the remote server is also django-based, you can use the same validation (assuming that you're not using CSRF that is -- I'm not certain whether it's possible to use CSRF to point to another server, mostly since that is part of the whole point of CSRF).

So, what is your real goal? Is there some second server (not the django one) that needs the file? If so, I'd process the upload on the Django server and perform the validation, then use a connection like ftp, sftp, scp, etc. to copy the file over to the other server.

Alternatively, you can write your own JavaScript (or existing JS framework) to validate the form and then submit the form to the remote server.

There's no need to use an iframe (empty or not), by the way. That's only necessary if your goal is to do an asynchronous submit of the file without leaving the page.

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