Reputation: 986
I have succeeded in uploading image into Database using AngularJS and Spring Mvc.But now I need to add form feilds and submit it simultaneously. Please do help me.Here is my code.
controller.js
scope.product = {};
scope.addProduct=function(product){
var formData=new FormData();
formData.append("file",file.files[0]);
formData.append("product",angular.toJson(product));
http({
method: 'POST',
url: '/name/addProduct',
headers: { 'Content-Type': undefined},
data: formData,
})
.success(function(data, status) {
alert("Success ... " + status);
})
.error(function(data, status) {
alert("Error ... " + status);
});
};
Controller.java
@RequestMapping(value = "/addProduct",consumes = {"multipart/form-data"}, method = RequestMethod.POST)
@ResponseBody
public Product addProduct(Product product,
@RequestPart("file") MultipartFile file,
HttpSession session, HttpServletRequest request,
HttpServletResponse response)
throws IOException, SerialException, SQLException{
byte[] mediaBytes = file.getBytes();
product.setImage(mediaBytes);
Product product2 = adminService.addProduct(product);
return product2;
}
Product.java
public class Product {
@Id
@Column(name="productId")
private Integer productId;
@Column(name="itemName")
private String itemName;
@ManyToOne
@JoinColumn(name="categoryId",insertable=true, updatable=true,
nullable=false)
//@JsonManagedReference
private Category categoryId;
@Column(name="image",columnDefinition="mediumblob")
private byte[] image;
@Transient
private String statusMessage;
@Transient
private Long CategoryValue;
//getters and setters
}
addItem.html
<form name="myForm" role="form" class="addItem" autocomplete="off"
id="addItem" enctype="multipart/form-data">
<input type="text" class="form-control" name="productId" ng-model="product.productId" placeholder="Product Id" ng-minlength="4" maxlength="4" required />
<input class="form-control" name="itemName" type="text" ng-model="product.itemName" required />
<input type="text" class="form-control" name="categoryValue" ng-model="product.categoryValue" required />
<input type="file" name="file" id="file" />
<input type="submit" name="Submit" class="btn btn-primary"
value="Submit" data-ng-disabled="myForm.$invalid"
ng- click="myForm.$invalid=true;addProduct(product)" />
</form>
Stacktrace
ServletInvocableHandlerMethod:164 - Failed to resolve argument 0 of type 'com.tta.abcd.model.Product'
org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part 'properties' is not present
at org.springframework.web.multipart.support.RequestPartServletServerHttpRequest.<init>(RequestPartServletServerHttpRequest.java:70)
at org.springframework.web.servlet.mvc.method.annotation.RequestPartMethodArgumentResolver.resolveArgument(RequestPartMethodArgumentResolver.java:126)
update
After removing @RequestPart("properties") am getting product data as null in controller.
I have posted here after trying many ways. Please help me.
Thank you.
Upvotes: 0
Views: 590
Reputation: 21
formData.append("file",file.files[0]);
formData.append("user",new Blob([JSON.stringify(user)],{type: "application/json"}));
http({
method: 'POST',
url: '/projectName/url',
headers: { 'Content-Type': undefined},//to set boundary value by default
data: formData,
})
Upvotes: 1
Reputation: 986
I have fixed my issue.Am posting changes i have made,If incase anyone needs it.
Controller.js
formData.append("file",file.files[0]);
formData.append("product",new Blob([JSON.stringify(product)],{type: "application/json"}));
Controller.java
public Product addProduct(@RequestPart("file") MultipartFile file,
@RequestPart("product")Product product,
HttpSession session, HttpServletRequest request,
HttpServletResponse response)throws IOException, SerialException, SQLException{
byte[] mediaBytes = file.getBytes();
product.setImage(mediaBytes);
Product product2 = adminService.addProduct(product);
return product;
}
After these changes,My code worked perfect.
Upvotes: 1
Reputation: 7682
Your variable names don't add upp.
In angular
formData.append("product",angular.toJson(product))
but in spring you've called it
@RequestPart("properties") Product product
Could that be the problem?
Upvotes: 1
Reputation:
You have to mention the below bean id in your spring context file.
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="maxUploadSize" value="10000000"/></bean>
And if you are using maven, add the dependencies as well.
<!-- Apache Commons FileUpload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
<!-- Apache Commons IO -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
Upvotes: 1