简体   繁体   English

在iframe中上传非阻止文件

[英]Non Blocking File Upload in Iframe

I have a button which when clicked opens up an overlay which is a form in an iframe. 我有一个按钮,当单击该按钮时会打开一个叠加层,这是iframe中的一种形式。 There is a file upload, I succeeded in making the overlay disappear and redirect the parent window to another page on submit. 有一个文件上传,我成功使覆盖消失,并将父窗口重定向到提交时的另一个页面。 The problem is here: 问题在这里:

If the user uploads a huge file the file has to be uploaded first before the user is redirected. 如果用户上载了巨大的文件,则必须先上载文件,然后再重定向用户。 Is there another way where the file is uploaded and at the same time the overlay is closed and the parent window redirected without loosing the file upload? 有没有其他方法可以上传文件,同时关闭叠加层并重定向父窗口,而又不会丢失文件上传?

Kind Regards, 亲切的问候,

For me the solution is quite simple. 对我来说,解决方案非常简单。 Don't use an iframe for the file upload. 不要使用iframe上传文件。 Just put your form with the file upload dialog into the main page. 只需将带有文件上传对话框的表单放入主页即可。 If the upload is finished the server can send a redirect back to the client and then the user is redirected to the new site. 如果上传完成,服务器可以将重定向发送回客户端,然后将用户重定向到新站点。 Or is there any problem with such simple solution? 还是这种简单的解决方案有任何问题?

UPDATE Because the simple solution doesn't work I suggest another solution. 更新因为简单的解决方案不起作用,所以我建议另一个解决方案。 Add an onload event listener to the iframe. 将onload事件侦听器添加到iframe。 If the event handler of the onload event is called (that means the file upload is finished) then redirect the user. 如果调用了onload事件的事件处理程序(这意味着文件上传已完成),那么将重定向用户。 To redirect the user you can use window.location.href = "your new url"; 要重定向用户,您可以使用window.location.href = "your new url"; But be aware that you add the event listener after the file form of the iframe is completely loaded or you have to ignore the first fired event (which would be the loaded file form) in you event handler. 但是请注意,在完全加载iframe的文件格式之后,您需要添加事件侦听器,否则您必须忽略事件处理程序中的第一个触发事件(将是加载的文件格式)。

just try this ... 试试这个...

  1. when the form submission is completed(means file uploaded) in server side you need to set a session flag like $_SESSION['file_uploaded'] = TRUE; 在服务器端完成表单提交后(意味着文件已上传),您需要设置一个会话标志,例如$ _SESSION ['file_uploaded'] = TRUE;

Before the step1 in parent window you need to write an ajax function to check the session. 在父窗口的step1之前,您需要编写一个ajax函数来检查会话。 That js ajax function continuously contact the server after the overlay(iframe) loaded. 加载了overlay(iframe)之后,该js ajax函数会持续与服务器联系。
when the server returns true you can hide the overlay and then redirect the page to some other location. 当服务器返回true时,您可以隐藏叠加层,然后将页面重定向到其他位置。

For this purpose I use either Valum's plugin or jQuery form plugin . 为此,我使用Valum的插件或jQuery form插件

If you insist on iframe file upload, see older version of Valum's AJAX file upload . 如果您坚持要上传iframe文件,请参阅Valum的AJAX文件上传的旧版本。 Either use his package, or see the code for inspiration. 使用他的软件包,或查看代码以获取灵感。 Don't get confused by the name; 不要被名字弄糊涂了; it doesn't use AJAX but iframe for file uploading. 它不使用AJAX,而是使用iframe上传文件。

Note that the newer version of Valum's plugin probably uses AJAX also. 请注意, Valum插件较新版本也可能使用AJAX。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM