Phantomjs 在状态机之外使用casperjs以多部分形式上传二进制文件(不能使用fill)

Phantomjs 在状态机之外使用casperjs以多部分形式上传二进制文件(不能使用fill),phantomjs,casperjs,telegram-bot,Phantomjs,Casperjs,Telegram Bot,更新1:我已经用测试夹具中实际运行的代码创建了一个要点,以准确显示我遇到的问题。我已经包括了工作机器人令牌(一个一次性机器人)和访问该机器人已经存在的电报聊天,以防有人想快速浏览。它是 更新2:我已经阅读了以下文章寻找答案(还有更多): 我有一个用casperjs(phantomjs)编写的程序,它通过BOT-API成功地将消息发送到Telegram,但我正在绞尽脑汁想如何发送照片 我可以从本地文件系统中以文件的形式访问我的照片,也可以以base64编码字符串的形式访问它(这是c

更新1:我已经用测试夹具中实际运行的代码创建了一个要点,以准确显示我遇到的问题。我已经包括了工作机器人令牌(一个一次性机器人)和访问该机器人已经存在的电报聊天,以防有人想快速浏览。它是


更新2:我已经阅读了以下文章寻找答案(还有更多):


我有一个用casperjs(phantomjs)编写的程序,它通过BOT-API成功地将消息发送到Telegram,但我正在绞尽脑汁想如何发送照片

我可以从本地文件系统中以文件的形式访问我的照片,也可以以base64编码字符串的形式访问它(这是casper屏幕截图)

我知道我的照片很好,因为我可以通过CURL发布它,使用:

curl -X POST "https://api.telegram.org/bot<token>/sendPhoto" -F chat_id=<id> -F photo=@/tmp/photo.png
同样,这是在CASPERJS环境中,而不是nodejs环境中,所以我没有类似fs.createReadableStream或File()构造函数的东西

function sendMultipartResponse(url, params) {
    var boundary = '-------------------' + Math.floor(Math.random() * Math.pow(10, 8));
    var content = [];

    for (var index in params) {
        content.push('--' + boundary + '\r\n');
        var mimeHeader = 'Content-Disposition: form-data; name="' + index + '";';
        if (params[index].filename)
            mimeHeader += ' filename="' + params[index].filename + '";';
        content.push(mimeHeader + '\r\n');
        if (params[index].type)
            content.push('Content-Type: ' + params[index].type + '\r\n');
        var data = params[index].content || params[index];
//        if (data.length !== undefined)
//            content.push('Content-Length: ' + data.length + '\r\n');
        content.push('' + '\r\n');
        content.push(data + '\r\n');
    };

    content.push('--' + boundary + '--' + '\r\n');
    utils.dump(content);

    var xhr = new XMLHttpRequest();
    xhr.open("POST", url, false);

    if (true) {
        /*
         * Heck, try making the whole thing a Blob to avoid string conversions
         */
        body = new Blob(content, {type: "multipart/form-data; boundary=" + boundary});
        utils.dump(body);
    } else {
        /*
         * this didn't work either, but both work perfectly for sendMessage
         */
        body = content.join('');
        xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
//        xhr.setRequestHeader("Content-Length", body.length);
    }

    xhr.send(body);
    casper.log(xhr.responseText, 'error');
};