gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:33:25 24 4
gpt4 key购买 nike

我有这段代码,以便使用 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>

运行代码后,我就可以上传文件了。但问题是,我每次都会在我的“上传”文件夹中上传 2 个文件。一个是原来的文件,一个是奇怪的名字,比如 2787ab2db292d90bd2da83a6a6ce1700。

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

最佳答案

问题是您正在从已上传的文件创建新文件。
所以解决方案是将具有临时名称的上传文件重命名为原始名称:

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 上传到临时文件夹,然后复制或移动到上传文件夹:

额外安装 fs:

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});
});
});

关于node.js - node.js上传文件,获取多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32794817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com