将/upload 网页(从 node.js 中的表单发布请求,npm 包 multer)重定向到另一个 html 页面 [英] Redirect the /upload webpage (got from a form-post request in node.js , npm package multer) to another html page

查看:72
本文介绍了将/upload 网页(从 node.js 中的表单发布请求,npm 包 multer)重定向到另一个 html 页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 node.js 还很陌生.我无法将页面/upload 重定向到另一个 .html 网页.我的意思是,一切正常,我上传了 img 文件,代码转到 http://localhost:3000/upload 页面,但我不知道如何自动重定向到另一个页面.html文件

这是我的 HTML:

<form action="/upload" method="POST" enctype="multipart/form-data"><input name="myImage" type="file"><input class="file-path validate" type="text" placeholder="上传您的文件">

<button type="submit" class="btn">提交</button></表单>

这里是我的服务器文件 index.js

const express = require("express");const multer = require("multer");const path = require("path");const 存储 = multer.diskStorage({目的地:'./public/uploads',文件名:函数(请求,文件,cb){cb(null, file.fieldname + '-' + Date.now() + path.extname(file.originalname));}});//初始化上传const 上传 = multer({存储:存储,限制:{文件大小:1000000},文件过滤器:函数(请求,文件,cb){检查文件类型(文件,CB);}}).single('myImage');//单张图片函数 checkFileType(file, cb) {const 文件类型 =/jpeg|jpg|png|gif/;const extname = filetypes.test(path.extname(file.originalname).toLowerCase());const mimetype = filetypes.test(file.mimetype);if (mimetype && extname) {返回 cb(null, true);} 别的 {cb("错误:仅图像");}};const app = express();app.use(express.static('./public'));app.post('/upload', (req, res) => {上传(请求,资源,(错误)=> {如果(错误){res.json(错误);} 别的 {如果(req.file == 未定义){res.json("错误:没有选择文件")} 别的 {res.json(`/${req.file.filename}`);}}});});app.listen(3000, 函数(req, res) {console.log("你正在监听3000端口");});

我在 http://localhost:3000/upload 上登陆,然后我留下来.如何跳过它并将此页面自动重定向到另一个代码 html 页面?感谢您的帮助

解决方案

你只需要在你的路由下使用 res.redirect('/path/here/); 函数.

完整设置:

//依赖const multer = require('multer');//多磁盘存储配置const diskStorage = multer.diskStorage({目的地:'assets/profile_upload',文件名:(请求,文件,call_back)=>{//将日期添加到文件名或任何使//文件是唯一的,所以它不会被覆盖call_back(null, Date.now() + '_' + file.originalname);}});//创建Multer实例const 上传 = multer({ 存储:diskStorage });//图片上传//或 app.post()router.post('/upload-pic', upload.single('file'), (req, res) => {//重定向发生在这里.控制台日志(req.file);res.redirect('/你的/路径');});

//您的代码:app.post('/upload', (req, res) => { ...尝试做 app.post('/upload' ,upload.single('file'), (req,res) =>{ ...

I am pretty new in node.js. I am unable to redirect the page /upload to another .html webpage. 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. html file

Here is my 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

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. How is possible to skip it and redirect this page automatically to another code html page? Thanks for any help

解决方案

You just have to use the res.redirect('/path/here/); function under your route.

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) =>{ ...

这篇关于将/upload 网页(从 node.js 中的表单发布请求,npm 包 multer)重定向到另一个 html 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆