簡體   English   中英

將 /upload 網頁(從 node.js、npm package multer 中的表單發布請求中獲得)重定向到另一個 Z28A3689BE95C88DD5E7A37DB516AB084 頁面

[英]Redirect the /upload webpage (got from a form-post request in node.js , npm package multer) to another html page

我是 node.js 的新手。 我無法將頁面 /upload 重定向到 another.html 網頁。 我的意思是,一切正常,我上傳了 img 文件,代碼轉到http://localhost:3000/upload頁面,但我不知道如何自動重定向到另一個頁面。 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>

這里是我的服務器文件 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");
});

我登陸http://localhost:3000/upload並留在這里。 如何跳過它並將此頁面自動重定向到另一個代碼 html 頁面? 謝謝你的幫助

你只需要使用res.redirect('/path/here/); function 在您的路線下。

完整設置:

//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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM