简体   繁体   English

它不会停止上传文件

[英]It doesn't stop uploading a file

I have a application which you can access here .我有一个应用程序,您可以在此处访问。 If you open the application please click on the "Add" button a couple of times.如果您打开应用程序,请点击“添加”按钮几次。 This will add a new row into a table below.这将在下表中添加一个新行。 In each table row there is an AJAX file uploader.在每个表格行中都有一个 AJAX 文件上传器。

The problem I have is that if you click on the "Upload" button, it shows a loading bar but the problem is that the loading bar just doesn't go away.我遇到的问题是,如果您单击“上传”按钮,它会显示一个加载栏,但问题是加载栏不会消失 go。 What was suppose to happen is that the user clicks on 'Upload' and then it will display the file input again and buttons with a message above stating whether file was successfully loaded or not?假设发生的是用户单击“上传”,然后它将再次显示文件输入和上面带有消息的按钮,说明文件是否已成功加载?

Why is the loading bar never stop loading and how can I fix this?为什么加载栏永远不会停止加载,我该如何解决这个问题?

Below is the code of the file input appended in each row and the javascript functions which it is suppose to start and stop the uploading:下面是每行附加的文件输入代码和假设开始和停止上传的 javascript 函数:

<script type="text/javascript">


function insertQuestion(form) {   

    var $tbody = $('#qandatbl > tbody'); 
    var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
    var $image = $("<td class='image'></td>"); 


var $fileImage = $("<form action='upload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='startUpload(this);' >" + 
    "<p class='f1_upload_process' align='center'>Loading...<br/><img src='Images/loader.gif' /><br/></p><p class='f1_upload_form' align='center'><br/><label>" + 
    "File: <input name='fileImage' type='file' class='fileImage' /></label><br/><label><input type='submit' name='submitBtn' class='sbtn' value='Upload' /></label>" +
    "</p> <iframe class='upload_target' name='upload_target' src='#' style='wclassth:0;height:0;border:0px solclass #fff;'></iframe></form>");

    $image.append($fileImage);

    $tr.append($image);  
    $tbody.append($tr); 

}

function startUpload(source_form){
  $(source_form).find('.f1_upload_process').css('visibility','visible');
  $(source_form).find('.f1_upload_form').css('visibility','hidden');
      return true;
}

function stopUpload(success, source_form){
      var result = '';
      if (success == 1){
         result = '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
      }
      else {
         result = '<span class="emsg">There was an error during file upload!<\/span><br/><br/>';
      }
      $(source_form).find('.f1_upload_process').css('visibility','hidden');
      $(source_form).find('.f1_upload_form').html(result + '<label>File: <input name="fileImage" type="file"/><\/label><label><input type="submit" name="submitBtn" class="sbtn" value="Upload" /><\/label>');
      $(source_form).find('.f1_upload_form').css('visibility','visible');     
      return true;   
}
</script>

Your upload.php file is not passing in the source_form parameter for the stopUpload() function. This is what your PHP file is returning:您的 upload.php 文件未传入 stopUpload() function 的 source_form 参数。这是您的 PHP 文件返回的内容:

window.top.window.stopUpload(0);

It has the success parameter but nothing for source_form.它有成功参数,但没有 source_form 参数。 So these three lines of code in stopUpload() are not going to work right, because source_form is going to be undefined:所以 stopUpload() 中的这三行代码将无法正常工作,因为 source_form 将是未定义的:

$(source_form).find('.f1_upload_process').css('visibility','hidden');
$(source_form).find('.f1_upload_form').html(result + '<label>File: <input name="fileImage" type="file"/><\/label><label><input type="submit" name="submitBtn" class="sbtn" value="Upload" /><\/label>');
$(source_form).find('.f1_upload_form').css('visibility','visible');  

I've put together a fix that should work:我整理了一个应该有效的修复程序:

<script type="text/javascript">
 var sourceForm; 

function insertQuestion(form) {   

    var $tbody = $('#qandatbl > tbody'); 
    var $tr = $("<tr class='optionAndAnswer' align='center'></tr>");
    var $image = $("<td class='image'></td>"); 
    var $fileImage = $("<form action='upload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='startUpload(this);' >" + 
    "<p class='f1_upload_process' align='center'>Loading...<br/><img src='https://helios.hud.ac.uk/u0867587/Mobile_app/Images/loader.gif' /><br/></p><p class='f1_upload_form' align='center'><br/><label>" + 
    "File: <input name='fileImage' type='file' class='fileImage' /></label><br/><label><input type='submit' name='submitBtn' class='sbtn' value='Upload' /></label>" +
    "</p> <iframe class='upload_target' name='upload_target' src='#' style='wclassth:0;height:0;border:0px solclass #fff;'></iframe></form>");

    $image.append($fileImage);

    $tr.append($image);  
    $tbody.append($tr);      
}

function startUpload(source_form){
  $(source_form).find('.f1_upload_process').css('visibility','visible');
  $(source_form).find('.f1_upload_form').css('visibility','hidden');
  sourceForm = source_form;
  return true;
}

function stopUpload(success){
      var result = '';
      if (success == 1){
         result = '<span class="msg">The file was uploaded successfully!<\/span><br/><br/>';
      }
      else {
         result = '<span class="emsg">There was an error during file upload!<\/span><br/><br/>';
      }
      $(sourceForm).find('.f1_upload_process').css('visibility','hidden');
      $(sourceForm).find('.f1_upload_form').html(result + '<label>File: <input name="fileImage" type="file"/><\/label><label><input type="submit" name="submitBtn" class="sbtn" value="Upload" /><\/label>');
      $(sourceForm).find('.f1_upload_form').css('visibility','visible');     
      return true;   
}
</script>

This starts by creating a new global variable called sourceForm at the very top of the block.首先在块的最顶部创建一个名为 sourceForm 的新全局变量。 This variable will be used to store which form the user clicked the upload button on, it is set in the startUpload() function:此变量将用于存储用户在哪个表单上单击了上传按钮,它在 startUpload() function 中设置:

sourceForm = source_form;

So as soon as the user hits Upload we will have a reference to the form they are using through the sourceForm variable.因此,一旦用户点击上传,我们就会通过 sourceForm 变量获得对他们正在使用的表单的引用。 Then in stopUpload() you just use that new sourceForm variable to update the visibility and set the return message.然后在 stopUpload() 中,您只需使用新的 sourceForm 变量来更新可见性并设置返回消息。

As a side note, you should really look into using jQuery ajax() .作为旁注,您真的应该考虑使用jQuery ajax() Using the iframe to run the JavaScript from your PHP file seems kind of difficult and cumbersome.使用 iframe 从您的 PHP 文件运行 JavaScript 似乎有点困难和麻烦。 The ajax() method is so much easier. ajax() 方法要简单得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM