Angular 6 ng2-file-upload 带文件的发送列表
Angular 6 ng2-file-upload send list with file
我的 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);
};
}
如何发送数组?
您可以 JSON 将其字符串化:
form.append('PropertyList',JSON.stringify(this.product.productPropertyList));
我的 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);
};
}
如何发送数组?
您可以 JSON 将其字符串化:
form.append('PropertyList',JSON.stringify(this.product.productPropertyList));