简体   繁体   English

jQuery iframe文件上传-触发成功或失败

[英]jQuery iframe file upload - trigger success or fail

I have this javascript which uploads a selected file via the iframe. 我有这个JavaScript,可通过iframe上传选定的文件。 I need to be able to determine from the iframe whether the file was successfully uploaded or not. 我需要能够从iframe中确定文件是否成功上传。 The problem is that it could be 3 seconds to upload the file or 30 seconds. 问题在于,上传文件可能需要3秒,也可能需要30秒。 The best method I can think of to trigger a function when the upload completes from within the iframe.... how would I target the main window from within the child iframe? 我可以想到的最佳方法是,当从iframe内完成上传时触发一个函数。...如何从子iframe内定位主窗口?

                if (file === false) {
                    var newEl = that.reset(this);
                    //fix listener
                    triggerNotification('x', 'Unable to upload file, file type not accepted');
                } else {
                    var id = Math.floor(Math.random()*101);
                    var form = jQuery(this).hide().after("<div class='upload'><div class='file'>"+file+"</div><div class='status'><img src='/image/progress-bar.gif'/></div></div><iframe id='"+id+"' name='"+id+"' src='about:blank' width='0' height='0' style='display: block;'></iframe>").parents('form');
                    form.attr('target', id).submit();
                    console.log(jQuery('iframe', form));
                    console.log(jQuery('iframe', form).contents());
                    console.log(jQuery('iframe', form).contents().find('body'));
                    console.log(jQuery('iframe', form).contents().find('body').html());

                    jQuery('input[type="button"]', form.parents('.popup-panel')).show();

                    triggerNotification('check', 'File has been uploaded');

                    //form.replaceWith("<a href='/image/icons/accept.png' target='_blank'><img src='/image/icons/accept.png' style='max-width: 120px;'/></a><br/><span class='faded'>(Click to view actual size)</span>");
                }

解决方案是通过使用parent.jQuery...而不是window.jQuery...在iframe上触发页面上的JS函数window.jQuery...

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

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