【问题标题】:node.js formidable with express.jsnode.js 强大的 express.js
【发布时间】:2018-07-15 18:21:09
【问题描述】:

我是 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>

【问题讨论】:

    标签: html node.js express formidable


    【解决方案1】:

    我也是这方面的新手,但是 form.ejs 中的表单 enctype 应该在 &lt;form&gt; 标记中。 而不是:

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

    尝试:

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

    你现在应该有你的文件对象了。

    干杯,

    标记

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-16
      • 2018-12-31
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-07
      相关资源
      最近更新 更多