[英]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.