Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用node.js上载文件_Javascript_Node.js_Express - Fatal编程技术网

Javascript 使用node.js上载文件

Javascript 使用node.js上载文件,javascript,node.js,express,Javascript,Node.js,Express,为了上传node.js文件,我有以下代码: app.use(express.bodyParser()); // or, as `req.files` is only provided by the multipart middleware, you could // add just that if you're not concerned with parsing non-multipart uploads, // like: app.use(exp

为了上传node.js文件,我有以下代码:

    app.use(express.bodyParser());
    // or, as `req.files` is only provided by the multipart middleware, you could 
    // add just that if you're not concerned with parsing non-multipart uploads, 
    // like:
    app.use(express.multipart());

    app.get('/',function(req,res){
    fs.readFile('uploadHTML.html',function (err, data){
        res.writeHead(200, {'Content-Type': 'text/html','Content-Length':data.length});
        res.write(data);
        res.end();
    });

    });
    app.post('/upload',function(req,res)
    {
    console.log(req.files);
    fs.readFile(req.files.displayImage.path, function (err, data) {
      // ...
      var newPath = __dirname;
      fs.writeFile(newPath, data, function (err) {
        res.redirect("back");
      });
    });
 });
以下是HTML文件:

<html>
<head>
<title>Upload Example</title>
</head>
<body>

<form id="uploadForm"
      enctype="multipart/form-data"
      action="/upload"
      method="post">
  <input type="file" id="userPhotoInput" name="displayImage" />
  <input type="submit" value="Submit">
</form>

<span id="status" />
<img id="uploadedImage" />


</body>
</html>

原因可能是什么?

我建议您在node/express中使用awesome模块处理文件上传

var fileupload = require('fileupload').createFileUpload('/uploadDir').middleware

app.post('/upload', fileupload, function(req, res) {
  // files are now in the req.body object along with other form fields
  // files also get moved to the uploadDir specified
})

另一种上传文件的方法是使用类似的东西

玉模板

form.data(action='/user/register', method='post', class="long-fields", enctype='multipart/form-data')
  input(type="text" name="name")
  input(name='fileLogo', type='file')
  input(type="submit" value="Register")
控制器

formidable = require('formidable'); //file upload handling via form
uuid = require('node-uuid'); //Unique ID
path = require('path'); //Path compiler
fs = require('fs'); //FileSystem

var form = new formidable.IncomingForm();
form.keepExtensions = false;
form.maxFieldsSize = 2 * 1024 * 1024; //2mb

form.parse(req, function(err, fields, files) {

    console.log(fields);
    console.log(files);

    fs.readFile(files.fileLogo.path, function (err, data) {
        var pathNew = __dirname + '/../../uploads/' + uuid.v1() + path.extname(files.fileLogo.name)

        fs.writeFile(pathNew, data, function (err) {
            console.log('uploaded', pathNew);
        });
    });

    res.send(jade.renderFile( settings.pathLess + prefix + '/register.jade', {
        req : req
    }));
});

您是否看过以下内容:您使用的是什么版本的Express?
formidable = require('formidable'); //file upload handling via form
uuid = require('node-uuid'); //Unique ID
path = require('path'); //Path compiler
fs = require('fs'); //FileSystem

var form = new formidable.IncomingForm();
form.keepExtensions = false;
form.maxFieldsSize = 2 * 1024 * 1024; //2mb

form.parse(req, function(err, fields, files) {

    console.log(fields);
    console.log(files);

    fs.readFile(files.fileLogo.path, function (err, data) {
        var pathNew = __dirname + '/../../uploads/' + uuid.v1() + path.extname(files.fileLogo.name)

        fs.writeFile(pathNew, data, function (err) {
            console.log('uploaded', pathNew);
        });
    });

    res.send(jade.renderFile( settings.pathLess + prefix + '/register.jade', {
        req : req
    }));
});