dongxian1921 2012-02-15 13:18
浏览 37
已采纳

多个uploadify图片上传问题

I am using multiple uploadify instances on single page. But the problem is that when I submit the form,form gets submitted before the upload of all the files.

I tried event.preventDefault(); to prevent the form submission before the image upload. but I am not getting the solution. Please suggest me some way by which I can prevent the form submission until all the uploadify files gets uploaded.

Here is my function

$("#add_facility_form .form-submit").click(function(event){

        event.preventDefault();
      $('#gallary').uploadifyUpload();
      $('#brochures').uploadifyUpload();
      $('#award_logo').uploadifyUpload();
      $('#acc_logo').uploadifyUpload();
      $('#file_upload').uploadifyUpload();

       $("#add_facility_form").submit();
  });
  • 写回答

3条回答 默认 最新

  • douhao8456 2012-02-15 13:30
    关注

    I would suggest using classes for your file uploads - will reduce your jQuery code .. for example ...

    <input type="file" id="file1" name="file1" class="uploadifyfile" />
    <input type="file" id="file2" name="file2" class="uploadifyfile" />
    <input type="file" id="file3" name="file3" class="uploadifyfile" />
    

    Then your jQuery becomes :

    $("#add_facility_form").submit( function(event){
        $('.uploadifyfile').uploadifyUpload(); // uses class instead of multiple IDs
        if (numFilesUploaded < $(".uploadifyQueueItem").length) { return false; }
    });
    

    Then when you initialise the uploadify elements add an onComplete method to keep a track of the completed uploads :

    $(".uploadifyfile").each(function () {
        $(this).uploadify({
           'onComplete': function (event, queueId, fileObj, response, data) {
             incrementUploadedCount();
           }
        });
    });
    

    Then create a variable for keeping a track of the completed uploads then in the incrementUploadedCount function check that all have been complete, if they have submit the form

    // keep track of uploaded count 
    var numFilesUploaded = 0;
    
    function incrementUploadedCount() {
        numFilesUploaded++; // increment complete count
        // check if complete count matches number of uploadify elements
        if (numFilesUploaded == $(".uploadifyQueueItem").length) {
             // submit your form
             $("#add_facility_form").submit();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?