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条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料