[英]Redirect the /upload webpage (got from a form-post request in node.js , npm package multer) to another html page
I am pretty new in node.js.我是 node.js 的新手。 I am unable to redirect the page /upload to another.html webpage.我无法将页面 /upload 重定向到 another.html 网页。 I mean, everything works fine, I upload the img file and the code goes to http://localhost:3000/upload page but I don't know how to automatically redirect to another.我的意思是,一切正常,我上传了 img 文件,代码转到http://localhost:3000/upload页面,但我不知道如何自动重定向到另一个页面。 html file html文件
Here is my HTML:这是我的 HTML:
<div class="container">
<form action="/upload" method="POST" enctype="multipart/form-data">
<input name="myImage" type="file">
<input class="file-path validate" type="text" placeholder="Upload your file">
</div>
<button type="submit" class="btn">Submit</button>
</form>
</div>
and here my server file index.js这里是我的服务器文件 index.js
const express = require("express");
const multer = require("multer");
const path = require("path");
const storage = multer.diskStorage({
destination: './public/uploads',
filename: function(req, file, cb) {
cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));
}
});
//init upload
const upload = multer({
storage: storage,
limits: { filesize: 1000000 },
fileFilter: function(req, file, cb) {
checkFileType(file, cb);
}
}).single('myImage'); //single image
function checkFileType(file, cb) {
const filetypes = /jpeg|jpg|png|gif/;
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
const mimetype = filetypes.test(file.mimetype);
if (mimetype && extname) {
return cb(null, true);
} else {
cb("Error:images only");
}
};
const app = express();
app.use(express.static('./public'));
app.post('/upload', (req, res) => {
upload(req, res, (err) => {
if (err) {
res.json(err);
} else {
if (req.file == undefined) {
res.json("Error: No file selected")
} else {
res.json(`/${req.file.filename}`);
}
}
});
});
app.listen(3000, function(req, res) {
console.log("you are listening to port 3000");
});
I land on http://localhost:3000/upload and here I stay.我登陆http://localhost:3000/upload并留在这里。 How is possible to skip it and redirect this page automatically to another code html page?如何跳过它并将此页面自动重定向到另一个代码 html 页面? Thanks for any help谢谢你的帮助
You just have to use the res.redirect('/path/here/);
你只需要使用res.redirect('/path/here/);
function under your route. function 在您的路线下。
Full setup:完整设置:
//Dependencies
const multer = require('multer');
//Multer DiskStorage Config
const diskStorage = multer.diskStorage({
destination: 'assets/profile_upload',
filename: (req, file, call_back) => {
//Prepend date to the filename or anything that makes
//the file unique so it won't be overwritten
call_back(null, Date.now() + '_' + file.originalname);
}
});
//Create Multer Instance
const upload = multer({ storage: diskStorage });
//Picture upload
//or app.post()
router.post('/upload-pic', upload.single('file'), (req, res) => {
//The redirection happens here.
console.log(req.file);
res.redirect('/your/path');
});
//Your code:
app.post('/upload', (req, res) => { ...
try doing app.post('/upload' ,upload.single('file'), (req,res) =>{ ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.