繁体   English   中英

node.js 强大的 express.js

[英]node.js formidable with express.js

我是 node.js 的新手,并从训练营、网站等各种来源学习它。我想使用 node.js 中的强大模块和 express.js 框架上传文件。 每次我运行这段代码时,它都会显示错误....

  var oldpath = file.fileupload.path;
                                   ^
  TypeError: Cannot read property 'path' of undefined

我已经使用正文解析器来接收文件名。

Node.js 代码:

 var express = require("express"); var app = express(); var bodyParser = require("body-parser"); var formidable = require("formidable"); var fs = require("fs"); var PORT = process.env.PORT || 5000 app.set("view engine","ejs"); app.use(bodyParser.urlencoded({extended: true})); app.get("/", function(req, res){ res.render("form"); }); app.post("/fileupload", function(req, res){ var fileupload = req.body.filetoupload; var form = new formidable.IncomingForm(); form.parse(req, function(err, fields, files){ var oldpath = files.fileupload.path; var newpath = "C:/Users/ayush/"+files.fileupload.name; fs.rename(oldpath, newpath, function(err){ if(err) console.log(err); else{ res.write("File Uploaded"); res.end(); } }); }); }); app.listen(PORT, function(){ console.log("Server started"); });
 <:DOCTYPE html> <html> <head> <title>FileUpload</title> </head> <body> <form action="/fileupload" method="POST"> <label> File: <input type="file" name="filetoupload" enctype="multipart/form-data"> </label> <button>Submit</button> </form> </body> </html>

我也是新手,但 form.ejs 中的表单 enctype 应该在<form>标签中。 代替:

<form action="/fileupload" method="POST">

尝试:

<form action="/fileupload" method="POST" enctype="multipart/form-data">

您现在应该拥有您的文件对象。

干杯,

标记

这是一个完整的工作示例:

上传.js

'use strict';

const fss = require('fs')
const pth = require('path');
const exp = require('express');
const swg = require('swig');
const efm = require("formidable");
const app = exp();

const thm = swg.compileFile(pth.join(__dirname, '', 'upload.html'));
app.listen(9009);
app.get(`/`, async (q, r) => r.send(thm({ msg: "Select a File to Upload" })));
app.get(`/:msg`, async (q, r) => r.send(thm({ msg: q.params.msg })));
app.post('/upload', (r, q) => {
    var form = new efm.IncomingForm();

    form.parse(r, (e, p, f) => {
        let dir = pth.join(__dirname, '', '/media/');

        if (!fss.existsSync(dir)) {
            fss.mkdirSync(dir);
        }

        let nPth = dir + f.file.name;
        try {
            fss.accessSync(nPth, fss.F_OK);
            q.redirect("/File Exists");
        } catch (file_e) {
            let err = fss.renameSync(f.file.path, nPth);
            q.redirect(err ? "/Error" : "/File Uploaded");
        }
    });
});
  1. 您可以使用fss.access进行“ A-SYNC ”操作。
  2. 最好使用“ A-SYNC ”功能。

上传.html

<h3>{{msg}}</h3>
<br/>
<form action="upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit">
</form>

fileupload object 在文件中不存在,因此您收到未定义的错误。

要访问旧路径,请使用:

var oldpath = files.upload.filepath;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM