I am currently working on a project that involves collecting form data, including a file upload. I am trying to implement a feature where the uploaded file is automatically sent to a Slack channel upon submission of the form. Despite following the guidance provided in this answer: , I keep encountering the same error message, 'invalid_form_data'. While I have managed to send a message using the postMessage method successfully, my goal is to attach the file directly from the user's system and upload it to Slack using the files.upload function. Is there a way to achieve this?
Below is the code snippet:
upload(e) {
this.file = e.target.files[0];
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'multipart/form-data'
})
};
var formData = new FormData();
formData.append('token', SLACK_TOKEN);
formData.append('channels', SLACK_CHANNELS);
formData.append('file', this.file);
this.http.post<any>('https://slack.com/api/files.upload', formData, httpOptions).subscribe(
resp => { console.log(resp) },
error => { console.log(error) }
);
}
Additionally, here is an example of the test file object:
File: {
lastModified: 1606970395000
lastModifiedDate: Wed Dec 02 2020 23:39:55 GMT-0500 (Eastern Standard Time) {}
name: "IMG_0320.HEIC"
size: 1206160
type: "image/heic"
webkitRelativePath: ""
}