【问题标题】:Angular 6 ng2-file-upload send list with fileAngular 6 ng2-file-upload 带文件的发送列表
【发布时间】:2019-11-25 03:13:55
【问题描述】:

我的 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);
    };
  }

如何发送数组?

【问题讨论】:

    标签: angular ng2-file-upload


    【解决方案1】:

    你可以 JSON 字符串化它:

    form.append('PropertyList',JSON.stringify(this.product.productPropertyList));
    

    【讨论】:

    • 我也试过了,没用。你的意思是在服务器端手动反序列化 json 字符串?
    猜你喜欢
    • 1970-01-01
    • 2017-07-19
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多