作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这段代码,以便使用 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/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!