我一直在试图弄清楚当用户单击“取消”按钮时如何取消文件上传到服务器。似乎实现这一点的最常见方法可能是在用户单击“取消”按钮时删除 iframe。
我只想知道当用户单击“取消”按钮时,这是否是尝试停止将文件上传到服务器的好方法?如果用户确实单击了“取消”按钮,那么我仍然希望用户能够稍后上传另一个文件,如果他们愿意的话,所以我希望在删除 iframe 时它不会是永久性的。
如果这是最好的方法,那么有没有人知道在单击“取消”按钮时如何删除 iframe?
下面是开始上传的代码和存储取消按钮功能的代码。
function startImageUpload(imageuploadform){
$(imageuploadform).find('.imagef1_upload_process').css('visibility','visible');
$(imageuploadform).find('.imagef1_cancel').css('visibility','visible');
$(imageuploadform).find('.imagef1_upload_form').css('visibility','hidden');
sourceImageForm = imageuploadform;
$(".imageCancel").click(function() {
$('.upload_target').get(0).contentwindow
return stopImageUpload();
});
return true;
}
以下是文件停止上传时的代码:
function stopImageUpload(success, imagefilename){
$(sourceImageForm).find('.imagef1_upload_process').css('visibility','hidden');
$(sourceImageForm).find('.imagef1_cancel').css('visibility','hidden');
$(sourceImageForm).find('.imagef1_upload_form').html(result + '<label>Image File: <input name="fileImage" class="fileImage" type="file"/></label><br/><br/><label><input type="submit" name="submitImageBtn" class="sbtnimage" value="Upload" /></label><label>');
$(sourceImageForm).find('.imagef1_upload_form').css('visibility','visible');
return true;
}
表单代码也如下:
var $fileImage = $("<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='return startImageUpload(this);' class='imageuploadform' >" +
//Above is form tag
"<p class='imagef1_upload_process' align='center'>Loading...<br/><img src='Images/loader.gif' /><br/></p><p class='imagef1_upload_form' align='center'><br/><label>" +
//Above is where it displays loading bar while file is uploading
"Image File: <input name='fileImage' type='file' class='fileImage' /></label><br/><br/><label class='imagelbl'>" +
//Above is File Input
"<input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /></label>" +
//Above is Upload Button
"</p><p class='imagef1_cancel' align='center'><label>" +
"<input type='button' name='imageCancel' class='imageCancel' value='Cancel' /></label>" +
//Above is Cancel Button
"<iframe class='upload_target' name='upload_target' src='#' style='width:0;height:0;border:0px;solid;#fff;'></iframe></form>");
//Above is the iframe which does the transporting