如何从数据库下载图像?
我需要在数据库中上传不超过2 MB的文件和图像。但是下载图像有问题。所有下载的图像都将中断并且无法打开。文本文件没有此类问题。
以这种方式上传图像:
module.exports.upload = async function (req, res) {
const sysFileObj = {
COMMENTS: req.body.COMMENTS,
NAME: req.file.originalname,
MIMETYPE: req.file.mimetype,
FILE_CONTENT: req.file.buffer
};
try {
await SysFiles.create(sysFileObj);
res.status(201).json(sysFileObj);
} catch (e) {
errorHandler(res, e);
}
};
multer:
const multer = require('multer');
const storage = multer.memoryStorage()
let obj = {
storage: storage,
limits: {
fileSize: 1024 * 1024 * 2
}
};
var upload = multer(obj)
module.exports = upload;
这里下载时出现问题:
module.exports.download = async function (req, res) {
try {
let sysFile = await SysFiles.findById(req.params.SYSFILE_ID);
var fileContents = Buffer.from(sysFile._props.FILE_CONTENT);
var readStream = new stream.PassThrough();
readStream.end(fileContents);
res.set('Content-disposition', 'attachment; filename=' + sysFile._props.NAME);
res.set('Content-Type', sysFile._props.MIMETYPE);
readStream.pipe(res);
} catch (e) {
errorHandler(res, e);
}
};
我在做什么错?请告诉我。我必须立即说,我需要将图像上载到数据库,而没有指向将要存储图像的任何文件夹的任何链接。
回答如下:您必须存储图像内容-类型和图像数据
const formidable = require('formidable');
const _ = require('lodash');
const fs = require('fs');
// loading formidable library
let form = new formidable.IncomingForm();
// taking file extension
form.keepExtensions = true;
//processing for upload image
form.parse(request_data,(err,fields,files) => {
//if error when uploading image
if (err) {
return res.status(400).json({
err: 'Image could not bd uploaded'
})
}
// validating all fields without image
const {name, description, price, category, quantity, shipping} = fields;
if(!name || !description || !price || !category || !quantity || !shipping) {
return res.status(400).json({
err: 'All Fields are required'
})
}
// lets continue when there are no error
// form have available photo
if(files.photo){
//1 kb = 1000
//1 MB = 1000000
// check image size
if(files.photo.size > 2000000){
return res.status(400).json({
err: 'Image should be less than 2 MB'
})
}
// there are no error found then execute it
TAKING_AS_YOUR_VALRIABLE.data = fs.readFileSync(files.photo.path);
TAKING_AS_YOUR_VALRIABLE.contentType = files.photo.type
}
// finaly save product
//save it your own way
const FINAL_DATA = {...fields,...TAKING_AS_YOUR_VALRIABLE}
})
现在显示照片代码
if(TAKING_AS_YOUR_VALRIABLE.data){
res.set('Content-Type',TAKING_AS_YOUR_VALRIABLE.contentType);
res.send(TAKING_AS_YOUR_VALRIABLE.data);
}
如何从数据库下载图像?
我需要在数据库中上传不超过2 MB的文件和图像。但是下载图像有问题。所有下载的图像都将中断并且无法打开。文本文件没有此类问题。
以这种方式上传图像:
module.exports.upload = async function (req, res) {
const sysFileObj = {
COMMENTS: req.body.COMMENTS,
NAME: req.file.originalname,
MIMETYPE: req.file.mimetype,
FILE_CONTENT: req.file.buffer
};
try {
await SysFiles.create(sysFileObj);
res.status(201).json(sysFileObj);
} catch (e) {
errorHandler(res, e);
}
};
multer:
const multer = require('multer');
const storage = multer.memoryStorage()
let obj = {
storage: storage,
limits: {
fileSize: 1024 * 1024 * 2
}
};
var upload = multer(obj)
module.exports = upload;
这里下载时出现问题:
module.exports.download = async function (req, res) {
try {
let sysFile = await SysFiles.findById(req.params.SYSFILE_ID);
var fileContents = Buffer.from(sysFile._props.FILE_CONTENT);
var readStream = new stream.PassThrough();
readStream.end(fileContents);
res.set('Content-disposition', 'attachment; filename=' + sysFile._props.NAME);
res.set('Content-Type', sysFile._props.MIMETYPE);
readStream.pipe(res);
} catch (e) {
errorHandler(res, e);
}
};
我在做什么错?请告诉我。我必须立即说,我需要将图像上载到数据库,而没有指向将要存储图像的任何文件夹的任何链接。
回答如下:您必须存储图像内容-类型和图像数据
const formidable = require('formidable');
const _ = require('lodash');
const fs = require('fs');
// loading formidable library
let form = new formidable.IncomingForm();
// taking file extension
form.keepExtensions = true;
//processing for upload image
form.parse(request_data,(err,fields,files) => {
//if error when uploading image
if (err) {
return res.status(400).json({
err: 'Image could not bd uploaded'
})
}
// validating all fields without image
const {name, description, price, category, quantity, shipping} = fields;
if(!name || !description || !price || !category || !quantity || !shipping) {
return res.status(400).json({
err: 'All Fields are required'
})
}
// lets continue when there are no error
// form have available photo
if(files.photo){
//1 kb = 1000
//1 MB = 1000000
// check image size
if(files.photo.size > 2000000){
return res.status(400).json({
err: 'Image should be less than 2 MB'
})
}
// there are no error found then execute it
TAKING_AS_YOUR_VALRIABLE.data = fs.readFileSync(files.photo.path);
TAKING_AS_YOUR_VALRIABLE.contentType = files.photo.type
}
// finaly save product
//save it your own way
const FINAL_DATA = {...fields,...TAKING_AS_YOUR_VALRIABLE}
})
现在显示照片代码
if(TAKING_AS_YOUR_VALRIABLE.data){
res.set('Content-Type',TAKING_AS_YOUR_VALRIABLE.contentType);
res.send(TAKING_AS_YOUR_VALRIABLE.data);
}