gpt4 book ai didi

node.js - 将上传的文件存储到 Express Js 的文件系统中

转载 作者:太空宇宙 更新时间:2023-11-03 22:01:14 25 4
gpt4 key购买 nike

我是nodejs新手,想知道如何将文件放入我的系统后端,甚至上传到S3等。

这是文件对象:

req.body { homeColor: 'black',
guestColor: 'white',
thirdColor: 'red',
file:
{ webkitRelativePath: '',
lastModifiedDate: '2014-05-05T02:26:11.000Z',
name: '2014-05-05 10.26.11.jpg',
type: 'image/jpeg',
size: 1310720 },
title: 'tfuyiboinini' }

如何处理req.body.file以便可以物理保存它?

请帮忙,谢谢!

最佳答案

长话短说

var fs = require('fs');
app.post('/file-upload', function(req, res) {
var tmp_path = req.files.thumbnail.path;
var target_path = './public/images/' + req.files.thumbnail.name;
fs.rename(tmp_path, target_path, function(err) {
if (err) throw err;
fs.unlink(tmp_path, function() {
if (err) throw err;
res.send('File uploaded to: ' + target_path + ' - ' + req.files.thumbnail.size + ' bytes');
});
});
};

您可以在这里阅读更多相关信息: http://www.hacksparrow.com/handle-file-uploads-in-express-node-js.html

关于node.js - 将上传的文件存储到 Express Js 的文件系统中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23710355/

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