发送资产图像作为formdata angular 6

时间:2018-08-03 23:14:43

标签: angular freshdesk

问题。

尝试将数据发送到freshdesk API以通过Angular 6创建票证 这是我到目前为止所拥有的

httpOptions = {
headers: new HttpHeaders({
  'Authorization':  btoa('xxxxx@gmail.com:password')
})
};
URL =  'https://' + this.FD_ENDPOINT + '.freshdesk.com' + this.PATH;

createTicket(email, subject, description, status, priority) {
  const fields = {
  'email': email,
  'subject': subject,
  'description': description,
  'status': status,
  'priority': priority
  };
return this.http.post(this.URL, fields, this.httpOptions );

}

这可行,但是我也想发送附件。加上图像已经在我的资产文件夹中

从阅读示例代码中我可以收集到的信息

https://github.com/freshdesk/fresh-samples

使用unirest通过node js

    var PATH = "/api/v2/tickets";
    var enocoding_method = "base64";
    var auth = "Basic " + new Buffer(API_KEY + ":" + 'X').toString(enocoding_method);
    var URL =  "https://" + FD_ENDPOINT + ".freshdesk.com"+ PATH;

    var fields = {
      'email': 'email@yourdomain.com',
      'subject': 'Ticket subject',
      'description': 'Ticket description.',
      'status': 2,
      'priority': 1
    }

    var headers = {
      'Authorization': auth
    }
unirest.post(URL)
  .headers(headers)
  .field(fields)
  .attach('attachments[]', fs.createReadStream('/path/to/file1.ext'))
  .attach('attachments[]', fs.createReadStream('/path/to/file2.ext'))
  .end(function(response){
    console.log(response.body)
    console.log("Response Status : " + response.status)
    if(response.status == 201){
      console.log("Location Header : "+ response.headers['location'])
    }
    else{
      console.log("X-Request-Id :" + response.headers['x-request-id']);
    }
});

通过JQuery:

 var yourdomain = 'yourdomain'; // Your freshdesk domain name. Ex., yourcompany
        var api_key = 'API_KEY'; // Ref: https://support.freshdesk.com/support/solutions/articles/215517-how-to-find-your-api-key
        var formdata = new FormData();
        formdata.append('description', 'sample description');
        formdata.append('email', 'tom@outerspace.com');
        formdata.append('subject', 'Test subject');
        formdata.append('priority', '2');
        formdata.append('status', '2');
        formdata.append('attachments[]', $('#myFile')[0].files[0]);
        $.ajax(
          {
            url: "https://"+yourdomain+".freshdesk.com/api/v2/tickets",
            type: 'POST',
            contentType: false,
            processData: false,
            headers: {
              "Authorization": "Basic " + btoa(api_key + ":x")
            },
            data: formdata,
            success: function(data, textStatus, jqXHR) {
              $('#result').text('Success');
              $('#code').text(jqXHR.status);
              $('#response').html(JSON.stringify(data, null, "<br/>"));
            },
            error: function(jqXHR, tranStatus) {
              $('#result').text('Error');
              $('#code').text(jqXHR.status);
              x_request_id = jqXHR.getResponseHeader('X-Request-Id');
              response_text = jqXHR.responseText;
              $('#response').html(" Error Message : <b style='color: red'>"+response_text+"</b>.<br/> Your X-Request-Id is : <b>" + x_request_id + "</b>. Please contact support@freshdesk.com with this id for more information.");
            }
          }
        );
      }
    );

所以我想使用

之类的东西
'attachments[]', fs.createReadStream('/path/to/file1.ext')

但是问题是:我要附加的图像已经在资产文件夹中。

我尝试查找解决方案, 到目前为止,我

使用画布将图像转换为数据URL。

使用文件输入。

FileReader

有没有更好的方法可以使用angular做到这一点。

0 个答案:

没有答案