删除iframe会阻止文件上传吗?

时间:2012-05-04 22:27:58

标签: php javascript jquery

当用户点击“取消”按钮时,我一直试图弄清楚如何取消文件上传到服务器。似乎最常见的实现方法是在用户点击“取消”按钮时删除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   

1 个答案:

答案 0 :(得分:2)

如果你有一个按钮

HTML

<button id="cancel">Cancel</button>

和JavaScript

$("#cancel").click(function() {
    $("iframe[name='upload_target']").attr("src", "javascript:'<html></html>'");
    $("iframe[name='upload_target']").remove(); // not needed I think, but anyway it is good to keep the DOM clean
});

并且用户点击该按钮,iframe的src属性将重置为不会创建混合内容警告的变体