我有一个简单的代码,使用express fs
将选择的图像保存到文件系统exports.upload = function(req, res) {
var photoInfo = req.body,
file = req.files.file,
fileType = file.type.slice(6);
//attaching the fileType to photoInfo because fileType is part of req.files, not part of req.body
photoInfo.fileType = fileType;
var newPhoto = new Photo(photoInfo);
Photo.create(newPhoto, function(err, insertedPhotoInfo){
if (err) throw err;
var photoId = insertedPhotoInfo._id;
//create directory if it does not exist already
fs.exists(__directory, function(exists) {
if (!exists) {
fs.mkdir(__directory, function(err, data){
if (err) throw err;
});
}
});
//create the file with the unique id created by mongo as its name
// http://www.hacksparrow.com/handle-file-uploads-in-express-node-js.html
// get the temporary location of the file
var tmpPath = file.path;
// set where the file should actually exists - in this case it is in the "images" directory
// var targetPath = './uploads/' + req.files.file.name;
// move the file from the temporary location to the intended location
fs.rename(tmpPath, __directory + '/' + photoId + '.' + fileType, function(err){
if (err) throw err;
// var reader = new FileReader();
// delete the temporary file, so that the explicitly set temporary upload dir does not get filled with unwanted files
fs.unlink(tmpPath, function() {
if (err) throw err;
res.send(insertedPhotoInfo);
});
});
});
};
这在localhost上完美运行,但是一旦我部署到heroku,我就会收到ENOENT,打开错误。它似乎失去了对fs的访问权限。我认为这是因为heroku只有短暂的文件系统。
为此会有任何解决方法吗?或者我需要设置一个单独的vm /静态文件服务器?