用于与后端Servlet通信的服务。
$http({
method: 'POST',
url: 'http://localhost:8080/Integration/upload',
headers : {
'Content-Type': 'multipart/form-data'
},
data:{
file:$scope.file
},
transformRequest: function (data, headersGetter) {
var formData = new FormData();
angular.forEach(data, function (value, key) {
formData.append(key, value);
});
var headers = headersGetter();
delete headers['Content-Type'];
return formData;
}
})
我们是否需要自定义指令,并且任何人都可以解释transformrequest方法。预先感谢。
这是我尝试过的servlet代码,但是它不起作用。
for(FileItem item : multiparts){
if(!item.isFormField()){
String name = new File(item.getName()).getName();
item.write( new File(UPLOAD_DIRECTORY + File.separator + name));
}
}
//File uploaded successfully
request.setAttribute("message", "File Uploaded Successfully");
} catch (Exception ex) {
request.setAttribute("message", "File Upload Failed due to " + ex);
}
}else{
request.setAttribute("message",
"Sorry this Servlet only handles file upload request");
}