2015-01-15 45 views
0

我有一個web服務來上傳通過GRAILS實現的文件。如何使用Grails webservice通過jQuery Ajax上傳圖片

curl -X POST -H "Authorization: 53676c55f8014c5eb84624b49d9c9e74" -H "Content-Type: multipart/form-data" http://localhost:8080/project/api/profiles/11/uploadlogo -F "[email protected]:/my_image.jpg" 

如何可以在使用jQuery AJAX功能我消耗: 我可以使用curl使用此命令使用的Web服務?

我不喜歡這樣,但它沒有工作:

var files = []; 

$('input').change(function(e){    
    files = e.target.files;  
}); 

$('form').on('submit', function(e){ 

    e.stopPropagation(); 
    e.preventDefault(); 

    var data = new FormData(); 
    $.each(files, function(i, file) { 
    console.log(file) 
    data.append('img', $('input').val()); 
    }); 

    $.ajax({ 
    url: 'http://localhost:8080/hsp-main/api/profiles/11/uploadimage', 
    contentType: 'false', 
    type: 'post', 
    processData: false, 
    header: { 
     "Authorization": '88db1ec45c3f4882859d59df45900fd5' 
    }, 
    data: data 
    }).done(function(response){ 
     console.log(response); 
    }).fail(function(a, s, d){ 
     console.log(a, s, d); 
    }); 

    return false; 
}); 

回答

0

這是解決

  var data = new FormData(); 
      data.append("img", $('input[type="file"]')[0].files[0]); 

      $.ajax({ 
       url: 'http://64.15.146.86:9090/hsp-main-0.1/api/profiles/11/uploadimage', 
       cache:false, 
       processData:false, 
       contentType:false, 
       type:'POST', 
       success:function (data, status, req) { 
        console.log(req); 
       }, 
       error:function (req, status, error) { 
        console.log(req); 
       }, 
       headers: { 
        'Authorization': 'ec5ce080a64941c68a84cac8f28293dd' 
       }, 
       data: data 
      }).done(function(response){ 
       console.log(response); 
      }).fail(function(a, s, d){ 
       console.log(a, s, d); 
      });