我在Angular项目中有这段代码,并使用ng2-file-upload上传图像。
export class Product {
id: string;
name: string;
imageUrl: string;
productCategoryId: string;
shopId: string;
productPropertyList: ProductProperty[] = [];
}
export class ProductProperty {
propertyId: number;
propertyName: string;
productId: string;
propertyValue: string;
}
当我尝试通过文件发送数据时,服务器端的productPropertyList为空,而在发送之前它具有数据。
initializeFileUploader(categoryId, shopId) {
this.uploader = new FileUploader({
url: this.baseUrl + 'users/' + this.authService.decodedToken.nameid + '/products/CreateProduct',
additionalParameter: ['Url'],
authToken: 'Bearer ' + localStorage.getItem('token'),
isHTML5: true,
allowedFileType: ['image'],
autoUpload: false,
removeAfterUpload: true,
maxFileSize: 5 * 1024 * 1024
});
this.uploader.onAfterAddingFile = (file) => { file.withCredentials = false; };
this.uploader.onBuildItemForm = (item, form) => {
console.log(this.product.productPropertyList);
form.append('ProductCategoryId', categoryId);
form.append('ShopId', shopId);
form.append('Name', this.product.name);
form.append('PropertyList', this.product.productPropertyList);
};
}
如何发送数组?
答案 0 :(得分:1)
您可以对其进行JSON字符串化
form.append('PropertyList',JSON.stringify(this.product.productPropertyList));