在取消文件上传时,Internet Explorer对我造成了问题,但在所有其他浏览器中,取消文件时没有引起任何问题。
现在在其他浏览器中,如果我上传文件然后在上传过程中单击“取消”按钮,它将停止文件上传到服务器,显示取消消息,就是这样,没有其他事情发生在哪很棒。
但在互联网资源管理器中,如果我上传文件然后在上传过程中点击“取消”按钮,则会显示取消消息,但之后仍然会在后台上传文件,这意味着该文件将上传到服务器然后它将解除文件成功加载消息,替换取消消息。
我的问题是,我的代码中的问题导致Internet Explorer中的问题是它继续文件上传,即使我已经取消了uplaod。有谁知道如何解决这个问题?
下面是一个表格,其中包含文件输入以及上传和取消按钮;
<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target_image' onsubmit='return imageClickHandler(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>
<input type='reset' name='imageCancel' class='imageCancel' value='Cancel' /></label>
<iframe class='upload_target_image' name='upload_target_image' src='#' style='width:0px;height:0px;border:0px;solid;#fff;'></iframe></form>
下面是开始上传文件的功能,其中包含取消功能:
function startImageUpload(imageuploadform){
$(imageuploadform).find('.imagef1_cancel').css('visibility','visible');
sourceImageForm = imageuploadform;
$(imageuploadform).find(".imageCancel").on("click", function(event) {
$('.upload_target_image').get(0).contentwindow
$("iframe[name='upload_target_image']").attr("src", "javascript:'<html></html>'");
jQuery.ajax("cancelimage.php").done(function(data) {
return stopImageUpload(2);
});
});
return true;
}
以下是上传完成后显示相关消息的代码:
var imagecounter = 0;
function stopImageUpload(success, imagefilename){
var result = '';
imagecounter++;
if (success == 1){
result = '<span class="imagemsg'+imagecounter+'">The file was uploaded successfully</span>';
}
else if (success == 2){
result = '<span class="imagemsg'+imagecounter+'"> The file upload was cancelled</span>';
}
else {
result = '<span class="imagemsg'+imagecounter+'">There was an error during file upload</span>';
}
$(sourceImageForm).find('.imagef1_cancel').css('visibility','hidden');
$(sourceImageForm).find('.imagemsg').html(result);
$(sourceImageForm).find(".fileImage").replaceWith("<input type='file' class='fileImage' name='fileImage' />");
return true;
}
下面是上传文件的imageupload.php页面:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
session_start();
if ($_FILES['fileImage']['error'] === UPLOAD_ERR_OK) {
$result = 0;
if (getimagesize($_FILES['fileImage']['tmp_name'])) {
if ((($_FILES["fileImage"]["type"] == "image/gif") || ($_FILES["fileImage"]["type"] == "image/jpeg") || ($_FILES["fileImage"]["type"] == "image/pjpeg") || ($_FILES["fileImage"]["type"] == "image/jpg")) && ($_FILES['fileImage']['size'] > 0)) {
if (is_file("ImageFiles/" . $_FILES['fileImage']['name'])) {
$parts = explode(".", $_FILES['fileImage']['name']);
$ext = array_pop($parts);
$base = implode(".", $parts);
$n = 2;
while (is_file("ImageFiles/" . $base . "_" . $n . "." . $ext))
$n++;
$_FILES['fileImage']['name'] = $base . "_" . $n . "." . $ext;
move_uploaded_file($_FILES["fileImage"]["tmp_name"], "ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
}
else {
move_uploaded_file($_FILES["fileImage"]["tmp_name"], "ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
}
}
}
} else {
echo "Upload was not successful";
}
?>
<script language="javascript" type="text/javascript">window.top.stopImageUpload(<?php
echo $result;
?>, '<?php
echo $_FILES['fileImage']['name'];
?>');</script>
</body>
</html>
更新:
以下内容正确无误:
//startImageUpload
function startImageUpload(imageuploadform){
$(imageuploadform).find('.imagef1_cancel').css('visibility','visible');
sourceImageForm = imageuploadform;
$(imageuploadform).find(".imageCancel").on("click", function(event) {
$('.upload_target_image').get(0).contentwindow
$("iframe[name='upload_target_image']").attr("src", "javascript:'<html></html>'");
$request = $.ajax("cancelimage.php").done(function(data) {
return stopImageUpload(2);
});
});
return true;
}
//imageClickHandler
function imageClickHandler(imageuploadform){
if(imageValidation(imageuploadform)){
window.lastUploadImageIndex = $('.imageuploadform').index(imageuploadform);
return startImageUpload(imageuploadform);
$request.abort()
}
return false;
}
答案 0 :(得分:1)
我似乎错误地认为您使用ajax上传图片。 现在我看到完整的代码我意识到你只是允许表单提交,而ajax只是与接收取消的php进行通信。这意味着您无法使用ajax abort取消上传。你可以做的是用cancelimage.php替换iframe内容。我没有测试过这个
//startImageUpload
function startImageUpload(imageuploadform){
$(imageuploadform).find('.imagef1_cancel').css('visibility','visible');
sourceImageForm = imageuploadform; // not sure what this does
$(imageuploadform).find(".imageCancel").on("click", function(event) {
$('.upload_target_image').get(0).contentwindow; // not sure what this is doing....
$("iframe[name='upload_target_image']").on("load",function() {
stopImageUpload(2);
}).attr("src", "cancelimage.php")
});
return true;
}
//imageClickHandler
function imageClickHandler(imageuploadform){
if(imageValidation(imageuploadform)){
window.lastUploadImageIndex = $('.imageuploadform').index(imageuploadform);
return startImageUpload(imageuploadform);
}
return false;
}