使用jquery将文件上传到Slack API files.upload [英] Uploading file to Slack API files.upload with jquery

查看:139
本文介绍了使用jquery将文件上传到Slack API files.upload的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在网页上选择一个文件,然后通过Slack API将其发布到Slack中.

I'm trying to, on a webpage, select a file and post it in Slack via the Slack API.

我本来是在做

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));
});

因此,然后我转而尝试使用Ajax,这样我就不必包括require.

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);
            });
});

但这给了我控制台警告 Form contains a file input, but is missing method=POST and enctype=multipart/form-data on the form. The file will not be sent. 即使我具有这些属性(在添加属性之前也出现了错误),我仍然会收到错误.

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.

我的html是简单的html5输入标签(用于上传和提交按钮)

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"/>

简而言之,我试图在不启动服务器的情况下将文件发送给我(以任何方式,Slack,Github,电子邮件等).有可能吗?

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?

推荐答案

弄清楚了:

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);
}

这篇关于使用jquery将文件上传到Slack API files.upload的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆