自定义 Multer 存储 - 使用 Sharp 时的非法操作
Custom Multer storage - Illegal operation when using Sharp
我正在使用 Multer together with Sharp to store images uploaded as part of an HTML form. I want to resize and transform the images before storing them on the disk and found this 讨论如何做到这一点。
我以为我已经正确设置了所有内容,但是当我尝试上传图片时,我得到:
Error: EISDIR: illegal operation on a directory, open 'C:\...\uploads'
下面是我的代码:
Routes.js:
var multer = require('multer');
var customStorage = require(path.join(__dirname, 'customStorage.js'));
var upload = multer({
storage: new customStorage({
destination: function (req, file, cb) {
cb(null, path.join(__dirname, 'uploads'));
},
filename: function (req, file, cb) {
cb(null, Date.now());
}
}),
limits: { fileSize: 5000000 }
});
...
app.use('/upload', upload.single('file'), (req, res) => { ... });
customStorage.js:
var fs = require('fs');
var sharp = require('sharp');
function getDestination (req, file, cb) {
cb(null, '/dev/null'); // >Implying I use loonix
};
function customStorage (opts) {
this.getDestination = (opts.destination || getDestination);
};
customStorage.prototype._handleFile = function _handleFile(req, file, cb) {
this.getDestination(req, file, function (err, path) {
if (err) return cb(err);
var outStream = fs.createWriteStream(path);
var transform = sharp().resize(200, 200).background('white').embed().jpeg();
file.stream.pipe(transform).pipe(outStream);
outStream.on('error', cb);
outStream.on('finish', function () {
cb(null, {
path: path,
size: outStream.bytesWritten
});
});
});
};
customStorage.prototype._removeFile = function _removeFile(req, file, cb) {
fs.unlink(file.path, cb);
};
module.exports = function (opts) {
return new customStorage(opts);
};
错误错误:EISDIR:对目录的非法操作在此上下文中表示您将 Multer 的目标设置为一个目录,而它应该是目标文件的名称。
目的地设置在 Routes.js 的 cb(null, path.join(__dirname, 'uploads'));
行中。如果将此行更改为 cb(null, path.join(__dirname, 'myDirectory\mySubdirectory\', myFilename + '.jpg'))
之类的内容,它将起作用。
我正在使用 Multer together with Sharp to store images uploaded as part of an HTML form. I want to resize and transform the images before storing them on the disk and found this 讨论如何做到这一点。
我以为我已经正确设置了所有内容,但是当我尝试上传图片时,我得到:
Error: EISDIR: illegal operation on a directory, open 'C:\...\uploads'
下面是我的代码:
Routes.js:
var multer = require('multer');
var customStorage = require(path.join(__dirname, 'customStorage.js'));
var upload = multer({
storage: new customStorage({
destination: function (req, file, cb) {
cb(null, path.join(__dirname, 'uploads'));
},
filename: function (req, file, cb) {
cb(null, Date.now());
}
}),
limits: { fileSize: 5000000 }
});
...
app.use('/upload', upload.single('file'), (req, res) => { ... });
customStorage.js:
var fs = require('fs');
var sharp = require('sharp');
function getDestination (req, file, cb) {
cb(null, '/dev/null'); // >Implying I use loonix
};
function customStorage (opts) {
this.getDestination = (opts.destination || getDestination);
};
customStorage.prototype._handleFile = function _handleFile(req, file, cb) {
this.getDestination(req, file, function (err, path) {
if (err) return cb(err);
var outStream = fs.createWriteStream(path);
var transform = sharp().resize(200, 200).background('white').embed().jpeg();
file.stream.pipe(transform).pipe(outStream);
outStream.on('error', cb);
outStream.on('finish', function () {
cb(null, {
path: path,
size: outStream.bytesWritten
});
});
});
};
customStorage.prototype._removeFile = function _removeFile(req, file, cb) {
fs.unlink(file.path, cb);
};
module.exports = function (opts) {
return new customStorage(opts);
};
错误错误:EISDIR:对目录的非法操作在此上下文中表示您将 Multer 的目标设置为一个目录,而它应该是目标文件的名称。
目的地设置在 Routes.js 的 cb(null, path.join(__dirname, 'uploads'));
行中。如果将此行更改为 cb(null, path.join(__dirname, 'myDirectory\mySubdirectory\', myFilename + '.jpg'))
之类的内容,它将起作用。