Jquery文件上传手动触发添加功能

时间:2015-10-14 20:36:13

标签: jquery jquery-file-upload

我的JqueryFileUpload就像这样。

$(function() {
      $('.directUpload').find("input:file").each(function(i, elem) {
        var fileInput    = $(elem);
        var form         = $(fileInput.parents('form:first'));
        var submitButton = form.find('input[type="submit"]');
        var progressBar  = $("<div class='bar'></div>");
        var barContainer = $("<div class='progress'></div>").append(progressBar);
        fileInput.after(barContainer);
        fileInput.fileupload({
          fileInput:       fileInput,
          url:             null,
          type:            'POST',
          autoUpload:       false,
          formData:         null,
          paramName:        'file',
          dataType:         'XML',
          replaceFileInput: false,
          add: function (e, data) {
            var goUpload = true;
            // var length = data.files.length;
            // for (var i = 0; i < length; i++) {
              var uploadFile = data.files[0];
              if (!(/\.(pdf|doc|docx|xls|xlsx|png|jpg|ppt|pps|pptx|ppsx)$/i).test(uploadFile.name)) {
              sweetAlert("Uh oh!", "You must select pdf|doc|docx|xls|xlsx|png|jpg|ppt|pps|pptx|ppsx,  files only", "warning");
                goUpload = false;
              }
              if (uploadFile.size > 50000000) {
              sweetAlert("Uh oh!", "Max size is 50 MB", "warning");
              goUpload = false;
              }
              if ($("#upload_document_number").val() == "")
              {
                sweetAlert("Sorry", "Document number can't be empty", "warning");
                goUpload = false;
              }
            // }
            if (goUpload == true) {
                $("#upload_size_in_bytes").val(uploadFile.size);
                    $("#upload_file").removeClass("disabled");
                    $("#upload_file").on('click',function () {           
                        $("#upload_file").addClass("disabled");
                        $("#upload_document_number").prop('disabled', true);
                        $("#upload_title").prop('disabled', true);
                        $.ajax({
                                  dataType: "json",
                                  url: "/uploads/get_presigned_post/" + $("#course_data").data("semestercourseid") + "/" + $("#upload_upload_type").val() + "/" + $("#upload_document_number").val() + "/" + uploadFile.name.substr(uploadFile.name.lastIndexOf('.') + 1).toLowerCase(),
                                  success: function(presigned_post) {
                                    data.url = presigned_post.url;
                                data.formData = presigned_post.fields;
                                  data.submit();
                                  }
                                });            
               });
            }
          },
          progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            progressBar.css('width', progress + '%');
          },
          start: function (e) {
            submitButton.addClass('disabled');

            progressBar.
              css('background', 'green').
              css('display', 'block').
              css('width', '0%').
              text("Loading...");
          },
          done: function(e, data) {
            submitButton.removeClass('disabled');
            $("#upload_document_number").prop('disabled', false);
                  $("#upload_title").prop('disabled', false);
            progressBar.text("Uploading done");

            var key   = $(data.jqXHR.responseXML).find("Key").text();
            var url   = data.url + '/' + key;
            $("#upload_upload_url").val(url);
            $("#upload_file_name").val(key.substr(key.lastIndexOf('/') + 1));
            submitButton.click();
          },
          fail: function(e, data) {
            progressBar.
              css("background", "red").
              text("Failed");
          }
        });
      });
    });

我希望能够在其他事件上触发"add"部分。怎么办呢?

我已经搜索了但是我发现的所有内容都在讨论使用submit()调用来触发函数体内的上传。不是我想要的。

0 个答案:

没有答案