2017-08-26 66 views
0

我是jquery的初學者。我試圖發送一個文件對象,我從我的 HTML得到:是否可以通過這種方式發送文件對象?

<label for="photo" class="col-xs-2">Photo</label> 
<input id="photo" name="fileName" type="file" class="col-xs-4"/> 

這是我的JavaScript:

var file = $('#photo')[0].files[0]; 
console.log(file); 

var data = { name: name, dob: dob,fname: fname,mob: mob,mName: mName, Email: Email, NID: NID,password: password,District: District,Thana: Thana,gender: gender,file: file }; 
$.ajax({url: "singleUpload", 
       type: "POST", 
       data:data, 
       contentType: false, 
       success: function (response) 
       { 
       $("#academic").html(response); 
       console.log("ajax ok"); 
       }  
}); 

我不想使用新FORMDATA()或iframe。

任何幫助將不勝感激。

回答

1
<form> 
<label for="photo" class="col-xs-2">Photo</label> 
<input id="photo" name="fileName" type="file" class="col-xs-4"/> 
</form> 

$('form').on('submit', uploadFiles); 

// Catch the form submit and upload the files 
function uploadFiles(event) 
{ 
    var file = $('#photo')[0].files[0]; 
    console.log(file); 
    var Jsondata = { name: name, dob: dob,fname: fname,mob: mob,mName: mName, Email: Email, NID: NID,password: password,District: District,Thana: Thana,gender: gender,file: file }; 
    event.stopPropagation(); // Stop stuff happening 
    event.preventDefault(); // Totally stop stuff happening 

    // START A LOADING SPINNER HERE 

    // Create a formdata object and add the files 
    var data = new FormData(); 
    $.each(Jsondata , function(key, value) 
    { 
     data.append(key, value); 
    }); 

    $.ajax({ 
      url: 'singleUpload', 
      type: 'POST', 
      data: data, 
      cache: false, 
      dataType: 'json', 
      processData: false, // Don't process the files 
      contentType: false, // Set content type to false as jQuery will tell the server its a query string request 
      success: function(data, textStatus, jqXHR) 
      { 
       if(typeof data.error === 'undefined') 
       { 
        // Success so call function to process the form 
        submitForm(event, data); 
       } 
       else 
       { 
        // Handle errors here 
        console.log('ERRORS: ' + data.error); 
       } 
      }, 
      error: function(jqXHR, textStatus, errorThrown) 
      { 
       // Handle errors here 
       console.log('ERRORS: ' + textStatus); 
       // STOP LOADING SPINNER 
      } 
     }); 
    } 
-1
var data = { name: name, dob: dob,fname: fname,mob: mob,mName: mName, Email: Email, NID: NID,password: password,District: District,Thana: Thana,gender: gender,file: file }; 
$.ajax({url: "singleUpload", 
       type: "POST", 
       data:JSON.stringify(data), 
       dataType: 'json', 
       contentType: 'application/json; charset=utf-8', 
       success: function (response) 
       { 
       $("#academic").html(response); 
       console.log("ajax ok"); 
       }  
}); 

你可以通過使用該數據的對象:

+0

感謝誰回答JSON.stringify(數據)。 FormData對象似乎很有用。我通過formdata.append()方法完成了這項任務。我從formdata.get()方法獲得了formdata值。順便說一下,Spring的控制器非常棒。我通過@RequestParam(value =「keyname」)得到了spring控制器中的formdata鍵。由於我接受了一個答案,我無法發佈我的解決方案。希望它能幫助一些人。 – RidwanulHaque

相關問題