简体   繁体   English

如何从bot Emulator上传文件(.pdf,.jpg..jpeg)并转换为二进制(base64)而不将文件保存到本地驱动器?

[英]How to upload file (.pdf,.jpg..jpeg) from bot Emulator and convert to binary (base64) without saving file into local drive?

SDK Platform: Node.js SDK平台:Node.js

How can I upload file (.pdf,.jpg..jpeg) from bot Emulator and convert to binary (base64) without saving file into local drive.如何从bot Emulator上传文件(.pdf,.jpg..jpeg)并转换为二进制(base64)而不将文件保存到本地驱动器。

Code Example:代码示例:

var url = session.message.attachments[0].contentUrl;
var fileName=session.message.attachments[0].name;
var encodedData = new Buffer(fs.createWriteStream(url+"/"+fileName), 'binary').toString('base64');

something like above, but it is not working for me.类似上面的东西,但它对我不起作用。

Expected Behavior:预期行为:

We need to upload file from BOT emulator and convert file content into binary data.我们需要从 BOT 模拟器上传文件并将文件内容转换为二进制数据。

Actual Result:实际结果:

ERROR: ENOENT: no such file or directory, open 'D:\\User\\projects\\messages\\http:\\localhost:61058\\v3\\attachments\\ne1gmlim5kfg\\views\\original\\filename.pdf'错误:ENOENT:没有这样的文件或目录,打开“D:\\User\\projects\\messages\\http:\\localhost:61058\\v3\\attachments\\ne1gmlim5kfg\\views\\original\\filename.pdf”

Here appending current directory which is not required.此处附加不需要的当前目录。

For reference: https://github.com/Microsoft/BotBuilder/issues/3628供参考: https : //github.com/Microsoft/BotBuilder/issues/3628

Here's an example of downloading a file and base64 encoding it in memory:这是下载文件并在内存中对其进行 base64 编码的示例:

var bot = new builder.UniversalBot(connector, function (session) {
    var msg = session.message;
    if (msg.attachments.length) {

        // Message with attachment, proceed to download it.
        // Skype & MS Teams attachment URLs are secured by a JwtToken, so we need to pass the token from our bot.
        var attachment = msg.attachments[0];
        var fileDownload = checkRequiresToken(msg) ? requestWithToken(attachment.contentUrl) : request(attachment.contentUrl);

        fileDownload.then(
            function (response) {
                var base64String = new Buffer(response, 'binary').toString('base64');

                var echoImage = new builder.Message(session).text('You sent:').addAttachment({
                        contentType: attachment.contentType,
                        contentUrl: 'data:' + attachment.contentType + ';base64,' + base64String,
                        name: 'Uploaded Image'
                    });
                session.send(echoImage);

            }).catch(function (err) {
                console.log('Error downloading attachment:', { statusCode: err.statusCode, message: err.response.statusMessage });
            });
    } else {
        // No attachments were sent
        var reply = new builder.Message(session)
            .text('Hi there! This sample is intented to show how can I receive attachments but no attachment was sent to me. Please try again sending a new message with an attachment.');
        session.send(reply);
    }
});

// Request file with Authentication Header
var requestWithToken = function (url) {
    return obtainToken().then(function (token) {
        return request({
            url: url,
            headers: {
                'Authorization': 'Bearer ' + token,
                'Content-Type': 'application/octet-stream'
            }
        });
    });
};

// Promise for obtaining JWT Token (requested once)
var obtainToken = Promise.promisify(connector.getAccessToken.bind(connector));

var checkRequiresToken = function (message) {
    return message.source === 'skype' || message.source === 'msteams';
};

An entire project demonstrating this can be found here: https://github.com/nwhitmont/botframework-node-v3-receive-attachment可以在此处找到演示这一点的整个项目: https : //github.com/nwhitmont/botframework-node-v3-receive-attachment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将pdf文件转换为base64 - How to convert a pdf file into base64 NodeJS - 如何在不将文件保存在服务器上的情况下从 Base64 返回 PDF? - NodeJS - How to return a PDF from a Base64 without saving the file on server? 我想将文件上传到 Blob 存储,而不是作为流(缓冲区、base64)而是作为原始文件(jpg 、 png jpeg) - I want to upload files to Blob Storage NOT AS STREAMS (Buffer, base64) BUT AS original file (jpg , png jpeg) 将base64转换为图像文件(jpeg,jpg) - Converting base64 to image file (jpeg, jpg) 如何在不使用Base64的情况下在ElasticSearch中索引二进制文件 - How to index binary file in ElasticSearch without using Base64 如何在JavaScript中将文件从base64重构为pdf? - How to reconstruct file from base64 to pdf in javascript? 将 Base64 stream 转换为文件 object 而不保存 - Converting Base64 stream to file object without saving 如何解码base64解码文件并保存为pdf文件 - How to decode a base64 decoded file and save to a pdf file 将 base64 字符串数据转换为 PDF 文件以从 API 下载 - NodeJS - Convert base64 string of data into PDF file for download from API - NodeJS 二进制文件到base64 nodejs - Binary file to base64 nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM