1. 程式人生 > >formData用ajax上傳圖片

formData用ajax上傳圖片

源於http://blog.csdn.net/high2011/article/details/49679475

沒有用框架,直接用java實現上傳圖片小功能



function doUpload() {  
	alert($( "#uploadForm" )[0]);
     var formData = new FormData($('#uploadForm')[0]);  
     $.ajax({  
          url: 'upload' ,  //請求
          type: 'POST',  //請求方式
          data: formData,  //資料型別,二進位制檔案
          async: false,  //同步
          cache: false,  
          contentType: false,  
          processData: false,  
          success: function (msg) {  //成功返回
              alert('success'+msg);  
          },  
          error: function (msg) {  //失敗返回
              alert('error'+msg);  
          }  
     });  
}  

servlet用下面兩個類就可以獲取formData

FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);