问题描述
我试图在用户点击取消按钮时停止上传文件。为此,我想停止iframe内的传输。问题是能够正确编码。下面是我想要实现的代码,但我在这里收到此错误:
I am trying to stop a file from uploading when the user clicks on the "Cancel" button. To do this I am thinking of stopping the transport inside the iframe. The problem is be able to code this correctly. Below is the code of what I am trying to achieve but I am getting this error here:
$('.upload_target').contentWindow is undefined
如何才能首先解决这个问题,第二个如何解决以下代码是否正确编写,以便在单击取消按钮时,它会停止iframe中的transpart?
How can this first of all be error be fixed and second how can the code below be written correctly so that when the "Cancel" button is clicked on, it stops the transpart inside the iframe?
以下是startImageUpload()函数,它开始上传文件。在该函数中,该函数在单击时处理取消按钮;
Below is the startImageUpload() function where it starts uploading a file. Within that function is the function which handles the "Cancel" button when it is clicked;
function startImageUpload(imageuploadform){
$(".imageCancel").click(function() {
$('.upload_target').get(0).contentwindow
if ($('.upload_target').contentWindow.document.execCommand)
{ // IE browsers
$('.upload_target').contentWindow.document.execCommand('Stop');
}
else
{ // other browsers
$('.upload_target').contentWindow.stop();
}
return stopImageUpload();
});
return true;
}
以下是由iframe组成的表单代码:
Below is the form code which consists of the iframe:
var $fileImage = $("<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='return startImageUpload(this);' class='imageuploadform' >" +
"Image File: <input name='fileImage' type='file' class='fileImage' /></label><br/><br/><label class='imagelbl'>" +
"<input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /></label>" +
"</p><p class='imagef1_cancel' align='center'><label>" +
"<input type='button' name='imageCancel' class='imageCancel' value='Cancel' /></label></p>" +
"<iframe class='upload_target' name='upload_target' src='#' style='width:0;height:0;border:0px;solid;#fff;'></iframe></form>");
推荐答案
$('。upload_target ')
将返回一个包含该类所有元素的jQuery对象,因此contentWindow属性不存在
$('.upload_target')
will return a jQuery object containing all elements with that class, so the contentWindow property wont exists
$( .upload_target:eq(0)')。get()
将返回(首先但假设只有一个)元素本身,然后contentWindow应该存在
$(.upload_target:eq(0)').get()
will return the (first but assuming only one) element itself and then contentWindow should exists
或 $(。upload_target')[0]
如果您要定位单个元素,但ID会更高效
or $(.upload_target')[0]
if you are targetting a single element but then an ID would be more performant
这篇关于如何正确停止iframe内的传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!