我正在快递中处理用户个人资料,我需要上传用户图片。我已经搜索了很多,但没有得到很多关于此的信息。
所以这是迄今为止我所做的。 我的个人资料查看代码: -
<form method="post" name="profileform" class="profileform" enctype="multipart/form-data" action="">
<div class="form-group">
<label for="UserProfilePicture">Profile Picture:</label>
<input type="file" class="form-control" id="UserProfilePicture" class="UserProfilePicture disbalefield" name="file" placeholder="Click to add Profile Pic"></div>
<input type="submit" value="Submit" class="btn btn-default" class="userprofilesubmit" id="userprofilesubmit" />
</form>
这是我的server.js代码
app.use('/uploads', express.static(process.env.OPENSHIFT_DATA_DIR+'/uploads'));
app.use(express.limit('5mb'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/profile', function(req, res){
if(!req.session.logged_in){
res.redirect('/login');
}
console.log("dsadsa");
// Get the temporary location of the file
var tmp_path = req.files.file.path;
// Set where the file should actually exists - in this case it is in the "images" directory.
var target_path = process.env.OPENSHIFT_DATA_DIR + '/uploads/' + req.files.file.name;
// Move the file from the temporary location to the intended location
fs.rename(tmp_path, target_path, function(err) {
if (err){
throw err;
}
// Delete the temporary file, so that the explicitly set temporary upload dir does not get filled with unwanted files.
fs.unlink(tmp_path, function() {
if (err){
throw err;
}
//
console.log("reached");
res.send("uploaded");
});
});
});
对缩进感到抱歉。任何帮助将受到高度赞赏。