blueimp jQuery文件上传未被触发

时间:2012-08-09 18:11:33

标签: jquery jquery-plugins file-upload blueimp

我正在尝试使用blueimp文件上传插件来异步上传图片,但是当我尝试上传它们时没有任何反应。根本没有触发任何回调函数。我正在使用此函数将文件上传器绑定到文件输入。

var initFileUpload = function (gender) {
//Used to bind fileupload plugin and send files

//bind file upload
$('#add_form').fileupload({
    namespace: gender+'_image',
    singleFileUploads: true,
    formData: [{name:'gender', value: gender}],
    fileInput: $("#"+gender+'_image'), 
    done: function(e, data){
        alert('success '+data.result);
    }

});
//Bind event callbacks
$('#add_form').bind('fileuploadsend', function (e, data) {
    console.log('sent');
});
$('#add_form').bind('fileuploaddone', function (e, data) {
    console.log('done');
});
$('#add_form').bind('fileuploadfail', function (e, data) {
    console.log(data.result);
});
$('#add_form').bind('fileuploadalways', function (e, data) {
    console.log(data.result);
});

}

灵感来自多文件上传教程here.我这样做是因为我有3个不同的输入,名称不同。然后我在我的ajax调用中手动触发send函数,或尝试。

$.ajax({
      type: "POST",
      url: "/manage/processadditem",
      data: $("#add_form").serialize(),
      dataType: "html",
      success: function( response, textStatus, XHR )
      {
        if(response.indexOf('invalid') >= 0 || response.indexOf('Exception') >= 0){
            //Show them their errors
            alert(response);
        }
        else{
            //Upload item images
            //Send files
            $('#add_form').fileupload('send', {fileInput: $("#neutral_image"), url: '/manage/items/itemimage/test' });
            $('#add_form').fileupload('send', {fileInput: $("#male_image"), url: '/manage/items/itemimage/test' });
            $('#add_form').fileupload('send', {fileInput: $("#female_image"), url: '/manage/items/itemimage/test' });
            alert('should have uploaded image to cdn.completeset.com/items/test_nla.jpg if gender was neutral');
        }

      },
      error: function(jqXHR, textStatus, errorThrown){
        alert('An error occurred, please try again later.');
      }
    });

我在程序文件上传部分的文档here中找到了您可以使用send方法手动发送带有$('#fileupload').fileupload('send', {files: filesList});之类的行的文件,这就是我要用来发送的内容文件。不幸的是,我根本没有从任何回调函数得到任何响应,我不知道为什么。

2 个答案:

答案 0 :(得分:2)

我刚刚在这里使用了malsup表单插件。 http://malsup.com/jquery/form/#ajaxForm在与blueImp挣扎2天之后,我只用了几分钟就启动了这个。

答案 1 :(得分:0)

试试这个,您可以在这里下载工作示例;

jQuery Blueimp file uploader