使用node.js上传文件,获取多个文件

时间:2015-09-26 07:38:25

标签: node.js

我有这段代码是为了使用express和multer上传带有node.js的单个文件:

var express = require("express");
var app = express();
var fs = require("fs");
var multer  = require('multer');
var upload = multer({ dest: 'uploads/' });

// Process upload file
app.post('/file_upload', upload.single('single-file'), function(request, response) {

    var fileName = request.file.originalname;
    var filePath = request.file.path;
    var file = __dirname + "/uploads/" +  fileName;

    fs.readFile(filePath, function(err, data) {
        fs.writeFile(file, data, function(err) {
            if (err) {
                console.log(err);
            } else {
                responseData = {
                    'message' : 'File uploaded successfully',
                    'fileName' : fileName
                };
            }

            response.end(JSON.stringify(responseData));
        })
    });
});

这是HTML文件:

<!DOCTYPE html>
<html>
    <head>
        <title>File Uploading Form</title>
    </head>
    <body>
        <h3>File Upload:</h3>
        Select a file to upload: <br />
        <form action="http://localhost:8081/file_upload" method="POST" 
      enctype="multipart/form-data">
            <input type="file" name="single-file" size="50" />
            <br />
            <input type="submit" value="Upload File" />
        </form>
    </body>
</html>

运行代码后,我能够上传文件。 但问题是,我每次都会在我的“uploads”文件夹中上传2个文件。一个文件的原始名称,一个名称如2787ab2db292d90bd2da83a6a6ce1700。

这是正常的吗?如何在上传时删除其他文件?

1 个答案:

答案 0 :(得分:1)

问题是您正在从已上传的文件中创建新文件 因此,解决方案是使用临时名称将上传的文件重命名为原始名称:

var express = require("express");
var app = express();
var fs = require("fs");
var multer  = require('multer');
var uploadsFolder = __dirname + '/uploads/';  // defining real upload path
var upload = multer({ dest: uploadsFolder }); // setting path for multer

// Process upload file
app.post('/file_upload', upload.single('single-file'), function(request, response) {

    var fileName = request.file.originalname; // original file name
    var file = request.file.path; // real file path with temporary name

    // renaming real file to it's original name
    fs.rename(file, uploadsFolder + fileName, function (err) {
      if (err) {
        console.log(err);
        response.json({success:false, message: err});
        return;
      }

      response.json({success:true, message: 'File uploaded successfully', fileName: fileName});
    });
});

或将multer上传到临时文件夹,然后复制或移动到uploads文件夹:

安装fs extra:

npm install --save fs.extra

var express = require("express");
var app = express();
var fs = require('fs.extra'); // extra functionality
var multer  = require('multer');
var uploadsFolder = __dirname + '/uploads/';  // defining real upload path
var tempFolder = __dirname + '/tmp/'; // folder for temporary files, must exist
var upload = multer({ dest: tempFolder }); // setting path for multer

// Process upload file
app.post('/file_upload', upload.single('single-file'), function(request, response) {

    var fileName = request.file.originalname; // original file name
    var file = request.file.path; // real file path with temporary name

    // renaming real file to it's original name
    fs.move(file, uploadsFolder + fileName, function (err) {
      if (err) {
        console.log(err);
        response.json({success:false, message: err});
        return;
      }

      response.json({success:true, message: 'File uploaded successfully', fileName: fileName});
    });
});