POST 上传完成之前重定向
我有带有文件上传的表格。上传的文件实际上是图片和视频,因此它们可能会很大。我有基于标头和前 1KB 的逻辑可以确定其余部分是否将被处理或立即拒绝。在后一种情况下,我想将客户端重定向到错误页面,而不必等待上传完成。
实际情况是,仅在 POST 完成之前发送响应似乎不起作用。重定向被忽略,如果我关闭连接,浏览器会抱怨“连接被对等方重置”错误。
所以问题是:是否有可能在纯 HTTP 中做到这一点(客户端无需 JavaScript),如果可以,如何实现?
I have form with file upload. The files to be uploaded actually are pictures and videos, so they can be quite big. I have logic which based on headers and first 1KB can determine if the rest will be processed or immediately rejected. In the later case I'd like to redirect client to error page without having to wait for upload to finish.
The case is, that just sending response before POST is complete doesn't seem to work. The redirect gets ignored and if I close connection, browser complains with "Connection reset by peer" error.
So the question is: is it even possible to do that in pure HTTP (without JavaScript on client-side), and if so, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
HTTP/1.1 协议确实允许这样做,只是以一种非常奇怪和混乱的方式。您需要应用以下 3 步过程:
这应该可以工作,因为如下所述,客户端在意外断开后至少会重试一次连接。在重试尝试时,预计仅发送标头,然后等待并观察错误响应,并在收到错误响应时中止发送正文。
http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html
遇到的问题:
在这种情况下?不是我。您需要运行一些测试。
The HTTP/1.1 protocol does allow for this, just in a really bizarre and messed up way. You need to apply the following 3 step proceedure:
This SHOULD work because as outlined below the client is expected to retry a connection at least once after being cut off unexpectedly. On the retry attempt(s) it is expected to only send the headers, then wait and watch for an error response and abort sending the body if it gets one.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html
Gotchas:
in this case? Not me. You'll need to run some tests.
看看 django 票证 #10850 - “无法在中途停止大文件上传”。不能解决问题,但至少可以帮助你理解它。
Have a look at django ticket #10850 - "Impossible to stop a large file upload mid-stream". Doesn't solve the problem, but at least it should help you understand it.
查看上传进度示例。
Checkout the uploadprogress examples.