I'm trying to, on a webpage, select a file and post it in Slack via the Slack API.
I was originally doing:
var request = require('request');
$(".submit").click(function(){
request.post({
url: 'https://slack.com/api/files.upload',
formData: {
token: "myTokenHere",
title: "Image",
filename: "image.png",
filetype: "auto",
channels: "mychannel",
file: document.getElementById('idofuploadelement').files[0]
},
}, function (err, response) {
console.log(JSON.parse(response.body));
});
So then I switched to trying Ajax so that I don't have to include require.
$(".submit").click(function(){
$.ajax({
type: "POST",
method: "POST",
enctype: "multipart/form-data",
url: 'https://slack.com/api/files.upload',
formData: {
token: "myTokenHere",
title: "Image",
filename: "image.png",
filetype: "auto",
channels: "mychannel",
file: document.getElementById('idofuploadelement').files[0]
}
}).done(function(url) {
console.log(url);
});
});
but this gives me the console warning
Form contains a file input, but is missing method=POST and enctype=multipart/form-data on the form. The file will not be sent.
Even though I have those properties (I got the error before adding them too), I still get the error.
My html is simple html5 input tags (for upload and submit button)
<input type="file" name="fileToUpload" id="idofuploadelement">
<input type="submit" name="submit" class="submit action-button next" value="Next"/>
In a nutshell, I'm trying to get a file sent to me (in any way, Slack, Github, email, whatever) without having to spin up a server. Is it possible?
Figured it out:
HTML:
JS: