Search code examples
javascriptjqueryajaxslackslack-api

Uploading file to Slack API files.upload with jquery


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?


Solution

  • Figured it out:

    HTML:

    <form id="myform" method="POST" enctype="multipart/form-data">
        <input type="file" name="fileToUpload" id="file-select">
        <input id="upload-button" type="submit" name="submit" class="submit action-button next" value="Next"/>
    </form>
    

    JS:

    var form = document.getElementById('myform');
    var fileSelect = document.getElementById('file-select');
    var uploadButton = document.getElementById('upload-button');
    form.onsubmit = function(event) {
        event.preventDefault();
        // Update button text.
        uploadButton.innerHTML = 'Uploading...';
        var file = fileSelect.files[0];
        var formData = new FormData();
        formData.append('file', file, file.name);
        formData.append('token', insert_token_here);
        formData.append('channels', insert_channel_here);
        var xhr = new XMLHttpRequest();
        xhr.open('POST','https://slack.com/api/files.upload', true);
    
        // Set up a handler for when the request finishes.
        xhr.onload = function () {
          if (xhr.status === 200) {
            // File(s) uploaded.
            uploadButton.innerHTML = 'Uploaded';
          } else {
            alert('An error occurred!');
          }
        };
        xhr.send(formData);
    }