[英]Slow file upload: Can I remove file attachment from multipart POST data on the fly and just submit form text fields?
I have a regular JSP/Servlet/Java web application that is used for uploading pictures from a mobile device.我有一个常规的 JSP/Servlet/Java Web 应用程序,用于从移动设备上传图片。 I am using Apache Commons library for the upload.我正在使用 Apache Commons 库进行上传。 Application is hosted on WebSphere Application Server 7.0.应用程序托管在 WebSphere Application Server 7.0 上。
Everything is working fine and the user can upload several images totaling 8MB or more if he has a really good/strong signal/connection or on a good WiFi.一切正常,如果用户拥有非常好的/强信号/连接或使用良好的 WiFi,他可以上传多张总计 8MB 或更多的图像。
The problem arises when the user is at a location with poor 3G/4G signal/connection.当用户位于 3G/4G 信号/连接较差的位置时,就会出现问题。 He gets errors like "Illegal state exception" or some time-out error, and in some cases the mobile browser just stays on the submit page with the progress bar no longer moving.他收到诸如“非法状态异常”或一些超时错误之类的错误,在某些情况下,移动浏览器只是停留在提交页面上,进度条不再移动。
Any suggestions on how to "gracefully" handle this?关于如何“优雅地”处理这个问题的任何建议? Like is there a way to intervene after a set amount of time and give the the user an option to submit the form without the file attachment (ie just submit the form text fields)?就像有没有办法在一段时间后进行干预并让用户选择提交没有文件附件的表单(即只提交表单文本字段)? Any other suggestions are welcome too.也欢迎任何其他建议。
UPDATE: The setTimeout solution below worked for me.更新:下面的 setTimeout 解决方案对我有用。 The other missing piece was that I have to issue a "browser stop" command to stop the original submission that's in progress before I can issue a re-submit.另一个缺失的部分是我必须发出“浏览器停止”命令来停止正在进行的原始提交,然后才能发出重新提交。 Otherwise, my re-submit command will just be ignored by the browser.否则,我的重新提交命令将被浏览器忽略。
The usecase here is simple - if the upload didn't finish in N minutes, remove/clear the field using javascript and resend the form.这里的用例很简单 - 如果上传未在 N 分钟内完成,请使用 javascript 删除/清除该字段并重新发送表单。
You don't need to control the upload in the basic implementation, just safely asume that if you set a timeout to resend, it won't happen if the first attempt was successful and the page reloaded.您不需要在基本实现中控制上传,只需安全地假设如果您设置了重新发送的超时时间,如果第一次尝试成功并重新加载页面,则不会发生这种情况。
jQuery pseudocode: jQuery 伪代码:
setTimeout(function(){
$imageFieldNode.remove();
$form.trigger('submit');
},30000);//after 30 seconds
The more advanced way is to use a ready solution for controlled upload.更高级的方法是使用现成的解决方案进行受控上传。 They work like that:他们是这样工作的:
You can do a lot with these libs.你可以用这些库做很多事情。
You can think about the approach used in popular webmail clients (when attaching files to a message):您可以考虑在流行的网络邮件客户端中使用的方法(将文件附加到邮件时):
The files are uploaded independently (ie before) of the form submit, using javascript.文件是独立(即之前)表单提交上传的,使用 javascript。 Each of the files are stored in a temporary directory, and after the upload succeeds the user can proceed with the action.每个文件都存储在一个临时目录中,上传成功后用户可以继续操作。 The upload status is displayed to the user, and if it fails the main action (form fill/submit) does not get interrupted.上传状态显示给用户,如果失败,主要操作(表单填写/提交)不会中断。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.