将列表添加到请求的参数中

时间:2016-12-12 22:50:34

标签: angular typescript ionic-framework ionic2

我正在使用以下功能将图像上传到本地服务器;

uploadPhoto() {
    let ft = new Transfer();
    let filename = this.imagePath.substr(this.imagePath.lastIndexOf('/') + 1);
    let options = {
      fileKey: 'file',
      fileName: filename,
      httpMethod: 'POST',
      mimeType: 'image/jpeg',
      chunkedMode: false,
      headers: {
        'Content-Type': undefined,
        'Content-Disposition': "attachment; filename=" + filename
      },
      params: {
        fileName: filename,
        users: this.user_list,
        name: this.event_name,
        event_type: this.event_type,
        date: this.date,
        start: this.timeStarts,
        location: this.location,
        info: this.description
      }
    };
    alert(options);
    ft.upload(this.imagePath, UPLOAD_URL, options, true)
      .then((result: any) => {
        this.imageChosen = 0;
        this.imagePath = '';
        alert(JSON.stringify(result));
      }).catch((error: any) => {
        alert(JSON.stringify(error));
      });
  }

当我检查服务器端时,请求不包含"用户"参数。但是,如果我像发送它一样;

users: JSON.stringfy(this.user_list)

我可以根据要求查看它,但我需要它作为列表而不是字符串。那么,有没有办法将其作为列表发送?

1 个答案:

答案 0 :(得分:0)

正如@Sakuto在上面的评论中提到的,你需要将你的params对象序列化为有效的JSON:

let params = {
  fileName: filename,
  users: this.user_list,
  name: this.event_name,
  event_type: this.event_type,
  date: this.date,
  start: this.timeStarts,
  location: this.location,
  info: this.description
}

...
params: JSON.stringfy(params)
...

在服务器上,您需要使用适用于技术堆栈的JSON实用程序方法对params进行反序列化。例如。对于节点:

params = JSON.parse(params);