使用 data-uri 和打字稿将图像转换为 base64
converting image to base64 with data-uri with typescript
我用multer成功上传图片,保存在本地目录,保存路径到数据库。但由于 heroku 不在本地目录中保存图像,我必须将图像上传到 Cloudinary,但 Cloudinary 需要将文件转换为 base64。
这是node.js中的实现:
const path = require("path");
const DataUri = require("datauri");
const dUri = new DataUri();
exports.dataUri = (file: Express.Multer.File): string =>
dUri.format(path.extname(file.originalname).toString(), file.buffer);
我试着用这样的打字稿来实现它:
import path from "path";
import DataUri from "datauri";
// here is cauinsg issue
const dUri = new DataUri();
export const dataUri = (file: Express.Multer.File): string =>
dUri.format(path.extname(file.originalname).toString(), file.buffer);
在 typescript 实现中,我收到 const dUri = new DataUri()
的 typescript 错误:
(alias) function DataUri(fileName: string, handler?: DataURI.Callback | undefined): Promise<string | undefined>
Expected 1-2 arguments, but got 0.ts(2554)
index.d.ts(2, 31): An argument for 'fileName' was not provided.
'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.
我不明白为什么 new DataUri()
在 olain node 中有效,但在 typescript 中无效。我认为这将是 ts 文件错误所以忽略它但它没有用。当我启动应用程序时出现此错误:“UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: “path”参数必须是字符串类型或 Buffer 或 URL 的实例。接收未定义”
我没有创建函数,而是尝试在控制器中这样实现:
const image = req.file;
const file64=new DataUri(image.buffer)
这也不起作用
老实说,我只使用过几次打字稿,但对于替代解决方案,您可以使用它。
在我使用 multer 使用纯 node.js 完成 base64 图像上传之前,它运行良好。我会把实际的部分留在这里,这样你就可以根据你的需要使用(只需在那里编辑你想要的):
routes/product.js
const productController = require('./controllers/product');
const { Router } = require('express');
const router = Router();
const multerUpload = require('./utilities/multer');
router.post('/form-data-upload', multerUpload.single('image'), productController.uploadFormDataImage);
router.post('/base64-upload', multerUpload.single('image'), productController.uploadBase64Image);
module.exports = router;
controllers/product.js
const fs = require('fs');
const path = require('path');
const port = process.env.APP_PORT;
const appUrl = process.env.APP_URL;
module.exports = {
uploadFormDataImage: (req, res, next) => {
return res.status(201).json({
error: false,
message: "Image was successfully uploaded.",
url: `${appUrl}:${port}/images/${req.file.filename}`
});
},
uploadBase64Image: async (req, res, next) => {
try {
const encoded = req.body.image;
const base64ToArray = encoded.split(";base64,");
const prefix = base64ToArray[0];
const extension = prefix.replace(/^data:image\//, '');
if (extension === 'jpeg' || extension === 'jpg' || extension === 'png')
{
const imageData = base64ToArray[1];
const fileName = (new Date().getTime() / 1000|0) + '.' + extension;
const imagePath = path.join(__dirname, './uploads/') + fileName;
fs.writeFileSync(imagePath, imageData, { encoding: 'base64' });
return res.status(201).json({
error: false,
message: "Base64 Image was successfully uploaded.",
url: `${appUrl}:${port}/images/${fileName}`
});
}
else {
return res.status(403).json({
error: true,
message: "Base64 data not valid!",
});
}
}
catch (e) {
return res.status(403).json({
error: true,
message: e.message,
});
}
},
};
utilities/multer.js
const multer = require('multer');
const path = require('path');
module.exports = multer({
storage: multer.diskStorage({
destination: function (req, file, cb) {
const filePath = path.join(__dirname, './uploads');
cb(null, filePath);
},
filename: function (req, file, cb) {
const extension = file.mimetype.split('/')[1];
const fileName = (new Date().getTime() / 1000 | 0) + '.' + extension;
cb(null, fileName);
}
}),
limits: {
fileSize: 1024 * 1024 * 10 // MB
},
fileFilter: (req, file, cb) => {
let valid = (file.mimetype === 'image/jpeg' || file.mimetype === 'image/jpg' || file.mimetype === 'image/png');
cb(null, valid);
},
});
构建 JS 过程中有一个读取 base64 文件的方法。有关 FileReader
的更多详细信息
const fr = new FileReader();
fr.onload = (data) => {
console.log(data.target.result);
// the result here will contain the base64 string.
}
fr.readAsDataURL(file);
这是我的解决方案:
import DatauriParser from "datauri/parser";
const parser = new DatauriParser();
因为我使用 multer 并将图像存储到内存中,所以我得到 req.file
和 req.file.buffer
const extName = path.extname(req.file.originalname).toString();
const file64 = parser.format(extName, req.file.buffer);
然后
const result = await Cloudinary.upload(file64.content!);
我用multer成功上传图片,保存在本地目录,保存路径到数据库。但由于 heroku 不在本地目录中保存图像,我必须将图像上传到 Cloudinary,但 Cloudinary 需要将文件转换为 base64。
这是node.js中的实现:
const path = require("path");
const DataUri = require("datauri");
const dUri = new DataUri();
exports.dataUri = (file: Express.Multer.File): string =>
dUri.format(path.extname(file.originalname).toString(), file.buffer);
我试着用这样的打字稿来实现它:
import path from "path";
import DataUri from "datauri";
// here is cauinsg issue
const dUri = new DataUri();
export const dataUri = (file: Express.Multer.File): string =>
dUri.format(path.extname(file.originalname).toString(), file.buffer);
在 typescript 实现中,我收到 const dUri = new DataUri()
的 typescript 错误:
(alias) function DataUri(fileName: string, handler?: DataURI.Callback | undefined): Promise<string | undefined>
Expected 1-2 arguments, but got 0.ts(2554)
index.d.ts(2, 31): An argument for 'fileName' was not provided.
'new' expression, whose target lacks a construct signature, implicitly has an 'any' type.
我不明白为什么 new DataUri()
在 olain node 中有效,但在 typescript 中无效。我认为这将是 ts 文件错误所以忽略它但它没有用。当我启动应用程序时出现此错误:“UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: “path”参数必须是字符串类型或 Buffer 或 URL 的实例。接收未定义”
我没有创建函数,而是尝试在控制器中这样实现:
const image = req.file;
const file64=new DataUri(image.buffer)
这也不起作用
老实说,我只使用过几次打字稿,但对于替代解决方案,您可以使用它。 在我使用 multer 使用纯 node.js 完成 base64 图像上传之前,它运行良好。我会把实际的部分留在这里,这样你就可以根据你的需要使用(只需在那里编辑你想要的):
routes/product.js
const productController = require('./controllers/product');
const { Router } = require('express');
const router = Router();
const multerUpload = require('./utilities/multer');
router.post('/form-data-upload', multerUpload.single('image'), productController.uploadFormDataImage);
router.post('/base64-upload', multerUpload.single('image'), productController.uploadBase64Image);
module.exports = router;
controllers/product.js
const fs = require('fs');
const path = require('path');
const port = process.env.APP_PORT;
const appUrl = process.env.APP_URL;
module.exports = {
uploadFormDataImage: (req, res, next) => {
return res.status(201).json({
error: false,
message: "Image was successfully uploaded.",
url: `${appUrl}:${port}/images/${req.file.filename}`
});
},
uploadBase64Image: async (req, res, next) => {
try {
const encoded = req.body.image;
const base64ToArray = encoded.split(";base64,");
const prefix = base64ToArray[0];
const extension = prefix.replace(/^data:image\//, '');
if (extension === 'jpeg' || extension === 'jpg' || extension === 'png')
{
const imageData = base64ToArray[1];
const fileName = (new Date().getTime() / 1000|0) + '.' + extension;
const imagePath = path.join(__dirname, './uploads/') + fileName;
fs.writeFileSync(imagePath, imageData, { encoding: 'base64' });
return res.status(201).json({
error: false,
message: "Base64 Image was successfully uploaded.",
url: `${appUrl}:${port}/images/${fileName}`
});
}
else {
return res.status(403).json({
error: true,
message: "Base64 data not valid!",
});
}
}
catch (e) {
return res.status(403).json({
error: true,
message: e.message,
});
}
},
};
utilities/multer.js
const multer = require('multer');
const path = require('path');
module.exports = multer({
storage: multer.diskStorage({
destination: function (req, file, cb) {
const filePath = path.join(__dirname, './uploads');
cb(null, filePath);
},
filename: function (req, file, cb) {
const extension = file.mimetype.split('/')[1];
const fileName = (new Date().getTime() / 1000 | 0) + '.' + extension;
cb(null, fileName);
}
}),
limits: {
fileSize: 1024 * 1024 * 10 // MB
},
fileFilter: (req, file, cb) => {
let valid = (file.mimetype === 'image/jpeg' || file.mimetype === 'image/jpg' || file.mimetype === 'image/png');
cb(null, valid);
},
});
构建 JS 过程中有一个读取 base64 文件的方法。有关 FileReader
的更多详细信息const fr = new FileReader();
fr.onload = (data) => {
console.log(data.target.result);
// the result here will contain the base64 string.
}
fr.readAsDataURL(file);
这是我的解决方案:
import DatauriParser from "datauri/parser";
const parser = new DatauriParser();
因为我使用 multer 并将图像存储到内存中,所以我得到 req.file
和 req.file.buffer
const extName = path.extname(req.file.originalname).toString();
const file64 = parser.format(extName, req.file.buffer);
然后
const result = await Cloudinary.upload(file64.content!);