我在字符串中有base64编码的二进制数据.
const contentType = 'image/png';
const b64Data = 'iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==';
Run Code Online (Sandbox Code Playgroud)
我想创建一个blob:包含此数据的URL并将其显示给用户.
const blob = new Blob(????, {type: contentType});
const blobUrl = URL.createObjectURL(blob);
window.location = blobUrl;
Run Code Online (Sandbox Code Playgroud)
我还没有弄清楚如何创建Blob.
在某些情况下,我可以通过使用data:URL 来避免这种情况.
const dataUrl = `data:${contentType};base64,${b64Data}`;
window.location = dataUrl;
Run Code Online (Sandbox Code Playgroud)
但是在大多数情况下,data:URL非常大.
如何Blob在JavaScript中将base64字符串解码为对象?
我正在使用nodejs保存base64图像,它将我的图像保存在确切的路径中,但显示错误。
请帮我找出错误。
这是我的代码
var express = require('express');
var router = express.Router();
const fs = require('fs');
const mime = require('mime');
const path = './uploads';
router.post('/register', (req, res, next) => {
const base64Image = req.body.image;
const matches = base64Image.match(/^data:([A-Za-z-+\/]+);base64,(.+)$/);
response = {};
if (matches.length !== 3) {
return new Error('Invalid input String');
}
response.type = matches[1];
response.data = new Buffer(matches[2]);
let decodedImg = response;
let imageBuffer = decodedImg.data;
let type = decodedImg.type;
let extension = mime.extension(type);
let fileName = 'image.' + extension; …Run Code Online (Sandbox Code Playgroud)