无法使用文件过滤器在快速 js 中重定向其他页面
Unable to use filefilter to redirect other page in express js
var express = require('express');
var router = express.Router();
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'public/uploads/');
},
filename: function (req, file, cb) {
cb(null, file.originalname);
}
});
var upload = multer({
storage: storage, fileFilter: function (req, file, cb) {
if (file.mimetype !== 'image/png' && file.mimetype !== 'image/jpg' && file.mimetype !== 'image/jpeg') {
return cb(null, false);
}
return cb(null, true);
}
}).any();
/* get home page. */
router.get('/', function (req, res) {
res.render('index', { title: 'express' });
});
router.post('/', function (req, res) {
upload(req, res, function (err) {
if (err) {
//I want to jump to another page
} else {
res.send(req.files);
}
});
});
module.exports = router;
在(错误)条件下,我想重定向我在视图文件夹下创建的页面,它被称为错误。但是,如果我使用 res.redirect('wrong') 或 res.redirect('views/wrong'),它们就不起作用。实际上我尝试了很多方法,但都不起作用。如果我上传文件但没有上传图片,它会跳转到一个包含一个"[]"的页面。如何进行重定向?
你可以使用这样的东西:
router.post('/', function (req, res) {
upload(req, res, function (err) {
if (err) {
res.redirect('/?error=upload_error');
} else {
res.send(req.files);
}
});
});
在页面中使用upload_error
来显示错误。
我得到了一个替代方案。在这种情况下,如果您上传错误的类型文件,它可能会显示错误消息。
var express = require('express');
var router = express.Router();
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'public/uploads/');
},
filename: function (req, file, cb) {
cb(null, file.originalname);
}
});
var upload = multer({
storage: storage, fileFilter: function (req, file, cb) {
if (file.mimetype !== 'image/png' && file.mimetype !== 'image/jpg' && file.mimetype !== 'image/jpeg') {
req.fileValidationError = 'goes wrong on the mimetype';
return cb(null, false, new Error('goes wrong on the mimetype'));
}
return cb(null, true);
}
}).any();
/* get home page. */
router.get('/', function (req, res) {
res.render('index', { title: 'express' });
});
router.post('/', function (req, res) {
upload(req, res, function (err) {
if (req.fileValidationError) {
//I want to jumpt to another page
res.send("You didn't upload a valid pic");
} else {
res.send(req.files);
}
});
});
module.exports = router;
相关文章:
- 角度 JS + 重定向不起作用
- node.js重定向无法添加用户名
- 节点JS重定向循环
- Hapi-js重定向到另一个带有额外请求数据的路由
- 使用来自不同页面的cookie创建URL,并使用JS重定向
- js重定向,如果不在fb iframe中
- 如何使用Meteor js重定向到Stripe成功后的成功页面
- 使用 PHP 设置的 cookie 在 js 重定向和新的 PHP 脚本使用 isset 检查后不可用
- 如何使用 PHP 而不是 JS 重定向用户
- 我想使用角度JS重定向到另一个页面
- 如何使用mongoDB和node.js重定向到查看页面
- 使用JS重定向
- Node.js重定向,带/不带尾部斜杠
- 如何将ember.js重定向到另一个页面?没有't使用transitionToRoute
- 如何使用JS重定向到具有哈希值的引用URL
- 什么时候可以使用JS重定向
- 如何使用js重定向访问者
- 提交表单后的节点JS重定向
- Express js重定向-dos't更改url并且不't加载静态文件
- JS重定向未按预期工作