简体   繁体   English

JSON对象和分段文件上传不起作用

[英]JSON object and Multipart file upload not working

We have Angular Controller as follows 我们有以下Angular Controller

$scope.uploadData = function (files, data) {
                var fd = new FormData();
                fd.append("CustomerName", "Mahesh"); //As of now mocking the entities for creating form data 
                fd.append("CustomerID ", "44444");
                fd.append("ProductList",  JSON.stringify([{ProductID: '0001', ProductName: 'Samsung'},{ProductID: '0002', ProductName: 'Voldats'}]));
                fd.append("file", files[0]);
                fd.append("file", files[1]);
                inventoryService.Postfile(fd);                    
            }

Restangular post as follows 矩形柱如下

Postfile : function (formData) {
                return restangular.all("postfile").withHttpConfig({transformRequest: angular.identity}).customPOST(formData, '', undefined, {'Content-Type': undefined});
                 },

Java VOs as follows Java VO如下

public class ProductList implements Serializable{
private String ProductID;
private String ProductName;

public String getProductID() {
    return ProductID;
}

public void setProductID(String productID) {
    ProductID = productID;
}

public String getProductName() {
    return ProductName;
}

public void setProductName(String productName) {
    ProductName = productName;
}

}

and

public class CustomerList implements Serializable{
String CustomerName;
String CustomerID;
List<ProductList> ProductList;

public String getCustomerName() {
    return CustomerName;
}

public void setCustomerName(String customerName) {
    CustomerName = customerName;
}

public String getCustomerID() {
    return CustomerID;
}

public void setCustomerID(String customerID) {
    CustomerID = customerID;
}

public List<ProductList> getProductList() {
    return ProductList;
}

public void setProductList(List<ProductList> productList) {
    ProductList = productList;
}
}

Spring controller as follows 弹簧控制器如下

@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/api/postfile", method = RequestMethod.POST)
public @ResponseBody
IhmsVO postfile(@RequestParam("file") List<MultipartFile> files,
        @ModelAttribute(value = "data") CustomerList vo, BindingResult bindingResult, Model model) {

    System.out.println("Post file");
    System.out.println(vo.getProductList());
    System.out.println("Files :: " + files + "  " + files.size());
    return null;
}

We have uploaded two files and JSON Object, CustomerName and CustomerID mapped properly with corresponding VO, but ProductList not mapped with the VO.We are getting null in ProductList, Debug mode screenshot attached for refrence. 我们已经上传了两个文件,并且JSON对象,CustomerName和CustomerID已与相应的VO正确映射,但ProductList没有与VO映射。 May help to resolve this? 可能有助于解决这个问题?

在此处输入图片说明

You have to use Blob with correct content type to pass the JSON data and add it to the form ( https://developer.mozilla.org/en/docs/Web/API/Blob ), eg: 您必须使用具有正确内容类型的Blob传递JSON数据并将其添加到表单( https://developer.mozilla.org/en/docs/Web/API/Blob ),例如:

fd.append("ProductList",  new Blob([ JSON.stringify([{ProductID: '0001', ProductName: 'Samsung'},{ProductID: '0002', ProductName: 'Voldats'}]) ], { type: "application/json" }));

EDIT: Updated the code to be absolutely clear and avoid any typos, case sensitivity issues, wrong placement of parenthesis etc. 编辑:更新了代码,使其绝对清楚,避免了任何错别字,大小写敏感性问题,括号的错误放置等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM