简体   繁体   English

NodeJS 将 base64 转换为八位字节流

[英]NodeJS convert base64 to octet-stream

I'm trying to access one API which received octet-stream as parameter.我正在尝试访问一个接收八位字节流作为参数的API For that I wanted to convert base64 string to octet-stream first.为此,我想首先将 base64 字符串转换为八位字节流。 In javascript I have achieved it using the default Blob type.在 javascript 中,我使用默认的 Blob 类型实现了 But in NodeJS I am not able to make it to perfect format which would be consumed by the API.但是在 NodeJS 中,我无法使其成为 API 使用的完美格式。

I have tried to convert it to Buffer and ArrayBuffers also, but no use.我也尝试将其转换为 Buffer 和 ArrayBuffers,但没有用。 I have also tried cross-blob library, but still it is not being converted to the exact format as I was getting in javaScript.我也尝试过跨 blob库,但它仍然没有像我在 javaScript 中那样被转换为确切的格式。

Try this :尝试这个 :

const fetch = require("node-fetch")
const fs  = require('fs');


let subscriptionKey = '<your key here>';
let endpoint = '<endpoint url of your vision serice>';
let filePath = '<path of your file>';

//code below is what you are looking for 
const base64 =  fs.readFileSync(filePath, 'base64') 
const data = Buffer.from(base64, 'base64')

if (!subscriptionKey) { throw new Error('Set your environment variables for your subscription key and endpoint.'); }

var uriBase = endpoint + "vision/v2.1/ocr";  //you define your API here , in this case I use ocr 

fetch(uriBase + "?" + {
    "language": "unk",
    "detectOrientation": "true",
},
{
method: 'POST',
headers: 
    {
    'Content-Type': 'application/octet-stream',
    'Ocp-Apim-Subscription-Key': subscriptionKey,
    },
body: data,    //post binary data directly, it applys to all post methods which data type is octet-stream in vision serice API 
}).then((response) => response.json()).then((data) =>
{
console.log(JSON.stringify(data, null, 2));
}).catch((error) =>
{
console.log(error);
});

Result :结果 :

在此处输入图片说明

Hope it helps.希望能帮助到你。

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

相关问题 使用javascript / jquery将application / octet-stream; base64转换为json字符串或简单文本 - Convert application/octet-stream;base64, to json String or Simple Text using javascript/jquery 如何将应用程序/八位字节流转换为要以 base64 或任何 jpeg 格式显示的图像? - How shall I convert application/octet-stream into image to be displayed in base64 or any jpeg format? 使用 firebase 的 putString() 上传 base64 png 会在存储中给我一个“类型:应用程序/八位字节流” - Uploading a base64 png with firebase's putString() gives me a 'type: application/octet-stream' in storage 将base64中的图像发送到Webservice - &#39;application / octet-stream&#39;不是预期的类型&#39;text / xml; 字符集= UTF-8&#39; - Sending image in base64 to Webservice - 'application/octet-stream' was not the expected type 'text/xml; charset=utf-8' 将八位字节流转换为图像 - Convert octet-stream to image 将 Api 响应(八位字节流)转换为 pdf - Convert Api response (octet-stream) to pdf 如何将八位字节流转换为图像 - How to convert octet-stream to images AJAX - 将返回的八位字节流转换为类型化数组 (Float64Array) - AJAX - Convert returned octet-stream to typed array (Float64Array) 八位字节流到 PNG - Octet-stream to PNG 无法将八位字节流的响应数据转换为 Zip 文件并下载 - Unable to convert response data of octet-stream to Zip file and download it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM