Javascript 将 blob 发送到电报

Javascript send blob to telegram

我想在电报中发送文件,我写了代码:

function send(file) {
    var formData = new FormData();
    formData.append('file', file, "2.txt");

    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'https://api.telegram.org/bot' + token + '/sendDocument?chat_id=' + id, true);

    xhr.send(formData);
}
var blob = new Blob(['hello'], {type: 'plain/text'});
console.log(blob)
blob.lastModifiedDate = new Date();
blob.name = '2.txt';
send(blob);

电报回复:

{"ok":false,"error_code":400,"description":"[Error]: Bad Request: there is no document in request"}

Telegram 期望文件所在的参数名为 document,而不是 file

function send(file) {
  var formData = new FormData();
  formData.append('document', file, '2.txt');

  // the rest of your code
}