我正在尝试使用AngularJS和Spring控制器上传文件。 Angular控制器如下所示:
$scope.uploadFile=function(){
var formData=new FormData();
formData.append("file",file.files[0]);
$http.post('/content-files/upload /', file.files[0], {
transformRequest: function(data, headersGetterFunction) {
return data; // do nothing! FormData is very good!
},
headers: {'Content-Type': undefined }
})
.success(function(){
console.log('Post Succeded !');
})
.error(function(){
console.log('Post Failed .');
});
}
我也试试这个:
var formData = new FormData();
formData.append('file',file.files[0]);
$http.post('/content-files/upload /', formData, {
headers: { 'Content-Type': undefined },
transformRequest: angular.identity
})
,Spring控制器如下所示:
@RequestMapping(value = "/content-files/upload/", method = RequestMethod.POST )
public @ResponseBody String handleFileUpload( @RequestParam("file") MultipartFile file) {
System.out.println("BrandController.uploadMultipart()");
String name=file.getName();
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
BufferedOutputStream stream =
new BufferedOutputStream(new FileOutputStream(new File(name)));
stream.write(bytes);
stream.close();
return "You successfully uploaded " + name + "!";
} catch (Exception e) {
return "You failed to upload " + name + " => " + e.getMessage();
}
} else {
return "You failed to upload " + name + " because the file was empty.";
}
}
我的html页面是:
<form enctype="multipart/form-data">
<input id="file-0a" class="file" type="file" file-model="myFile" name="myFile" />
<button ng-click="uploadFile()">upload me</button></form>
我在web-inf / lib中有2个罐子:commons-upload.jar和commons-io.jar 我在我的spring配置文件中添加了这个:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="50000000"/>
</bean>
当我尝试上传文件时,出现错误:
org.springframework.web.multipart.MultipartException: The current request is not a multipart request
当我尝试在我的spring函数而不是Multipart中使用MultipartHttpServletRequest上传文件时出现此错误:
java.lang.IllegalStateException: Current request is not of type [org.springframework.web.multipart.MultipartHttpServletRequest]: org.apache.catalina.connector.RequestFacade@196f5636
当我使用HttpServletRequest请求并尝试投射它时,我得到了一个ClassCastException。 当我从我的表格Tag
中取消enctype =“multipart / form-data”时没有任何变化答案 0 :(得分:2)
需要做两件事:
1. func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
return CGSize(width: cellWidth, height: cellWidth)
}
2.传递的数据应转换为URL编码的字符串
参考:
enter link description here
答案 1 :(得分:0)
我陷入了同样的问题。下面是角度代码
您应该做的事情:
- 从标题中删除Content-Type。
- 对于多文件上传,请使用以下代码
handleImageUpload = async (event) => {
if (event.target.files && event.target.files) {
const files = event.target.files;
_.map(files, (file) => {
//array for multiple files upload
this.imageData.push(file);
const reader = new FileReader();
//base64 encoded image for preview
reader.onload = async (event: any) => {
this.album.push(event.target.result);
};
reader.readAsDataURL(file);
});
this.uploadedImage = Promise.resolve(this.album);
}
}
在您提交处理程序时,请使用此代码
const formData: any = new FormData();
// adding multiple files
this.imageData.map((i) => {
formData.append("files", i);
});
// add objects
_.map(body,(value,key) => {
formData.append(key, value);
});
// delete headers it will be set by browser
headers = headers.delete("Content-Type");
// make a call to api
this.httpClient
.post(url, formData, { headers })
.subscribe((res) => console.log(res));