我正在使用ajax和rest web服务将文件上传到服务器。我已经捕获了文件上传事件,并在我的jquery方法中获取了文件对象。我必须将文件对象发送到web服务,这样我才能将文件保存到数据库。下面是我的jquery代码。
$scope.selectFile = function (fileObj) {
if(fileObj!=undefined){
$.ajax({
url : "/RestServices/services/uploadFile",
type : "POST",
data : fileObj,
processData: false,
contentType: false,
success : function(result) {
$scope.$apply();
},
error : function(xhr, tStatus, err) {
// alert(err);
}
});
}
我也尝试过使用FormData,但我无法在web服务中获取该文件。
var formData = new FormData();
formData.append("fileToUpload", fileObj);
if(fileObj!=undefined){
$.ajax({
url : "/RestServices/services/uploadFile",
type : "POST",
data : formData,
processData: false,
contentType: false,
success : function(result) {
$scope.$apply();
},
error : function(xhr, tStatus, err) {
// alert(err);
}
});
下面是我的服务中的java代码
@POST
@Path("/uploadFile")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void uploadFile(File formData) {
try {
System.out.println(formData.getFileName());
} catch (Exception e) {
LOGGER.error("Exception in Upload File Endpoint"+ e.getMessage());
}
}
如果我不使用formData直接发送文件对象,我会收到“不支持的媒体类型错误”,如果我发送formData到服务,我只能得到临时文件。ajax和服务方法中应该有什么数据类型?如何在服务中获取上传的文件?任何帮助都将不胜感激。
发布于 2016-01-18 20:17:01
我今天碰到了这个问题,我是这样解决的:
HTML
<input type="button" value="Upload document" class="btn btn-xs btn-primary uploadDocumentOnboarding">
<input id="file" type="file" class="findDocumentOnboarding">
Ajax/Jquery
$(".uploadDocumentGeneral").on("click", function (evt) {
var documentData = new FormData();
documentData.append('file', $('input#file.findDocumentOnboarding')[0].files[0]);
$.ajax({
url: url,
type: 'POST',
data: documentData,
cache: false,
contentType: false,
processData: false,
success: function (response) {
alert("Document uploaded successfully.");
}
});
return false;
});
JAVA
@POST
@Path("upload/{id}")
@Consumes({"application/x-www-form-urlencoded", "multipart/form-data"})
public void addBlob(@PathParam("id") Integer id, @FormDataParam("file") InputStream uploadedInputStream) throws IOException {
E24ClientTemp entityToMerge = find(id);
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int read = 0;
byte[] bytes = new byte[1024];
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
entityToMerge.setTestBlob(out.toByteArray());
super.edit(entityToMerge);
}
catch (IOException e) {
e.printStackTrace();
}
}
我知道有点晚了,但我看到问题还没有答案。
发布于 2015-03-05 15:30:39
上传文件的方法真的不同于仅仅是一个ajax请求。此外,考虑所有的旧浏览器真的很难。幸运的是,其他人已经为我们解决了这个问题。
我推荐使用像jQuery File Upload这样的jQuery插件,或者使用Dropzone,这太棒了。
发布于 2015-10-01 02:37:35
我让它这样工作:
//JAVASCRIPT
var objFile = $jQuery("#fileToUpload");
var file = objFile[0].files[0];
API.call({
url : "rest-url/upload",
type : "POST",
contentType : "multipart/form-data",
data: file,
processData: false
});
//JAVA
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@Path("rest-url/upload")
public Response upload(InputStream fileInputStream) throws Exception {
//here you got your file
return Response.ok().entity(new Object("response")).build();
}
https://stackoverflow.com/questions/25443350
复制相似问题