当用户单击“取消”按钮时,我试图阻止文件上传。为此,我正在考虑停止 iframe 内的传输。问题是能够正确编码。以下是我试图实现的代码,但我在这里收到此错误:
$('.upload_target').contentWindow is undefined
首先如何修复错误,其次如何正确编写下面的代码,以便当单击“取消”按钮时,它会停止 iframe 内的传输?
下面是开始上传文件的 startImageUpload() 函数。在该函数中是处理单击“取消”按钮的函数;
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 的表单代码:
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>");