NFS
通过浏览器上传到ftp,因为ftp和我们的node server部署在同一台服务器上,这里,我们直接通过前台文件上传到后台server 设置后台路径是ftp存储文件的路径即可
var express = require('express');
var fs = require('fs');
var path = require('path');
var fstool = require('../utils/fsTool.js');
var cp = require('child_process');
var exec = cp.execFile;
var moment = require('moment');
var dbcon = require("./mysqlconn.js");
var multiparty = require('connect-multiparty')({
uploadDir: process.env.TMPDIR
});
fstool.clearDir(process.env.TMPDIR);
function uploadFiles(req) {
// console.log(req.files,req.body);
var f = req.files.file || {};
var pt = require('path').relative(process.env.WEBROOT, f.path || "");
return {
url: pt,
file:f
};
}
function sendResult(res,status,data,msg){
var d = {status:status};
if(data) d.data = data;
if(msg) d.msg = d.error = msg;
res.status(status?200:500).send(d);
}
router.post('/default', multiparty, function(req, res) {
var file = uploadFiles(req);
if (file && file.url){
// var ext = path.extname(file.url)
// var timestamp = moment().format("YYYYMMDDHHmm");
var fname = req.files.file.originalFilename;
var sqlstr = "SELECT * FROM t_user WHERE nId='" + req.session.userId + "'";
dbcon(sqlstr, function(err, vals, fields) {
if (err) {
sendResult(res,0,null,'上传失败!数据库查询错误');
console.log(err);
} else {
// fstool.mkDirs(targetPath);
var uri = "mnt/bigdata/"+vals[0].strUserId + '/' + req.query.id +'/'+ fname;
console.log(uri);
var targetPath = process.env.MNTROOT+uri;
fstool.moveFile(file.file.path,targetPath,function(errs){
if(errs){
console.log(errs);
sendResult(res,0,null,'上传失败!移动文件失败');
} else {
var sliceExe = "./gpu_change_authority.sh";
exec(sliceExe, [vals[0].strUserId, req.query.id], {
cwd: getScriptPath()
},
function(error, stdout, stderr) {
sendResult(res,1,uri);
}
);
sendResult(res,1,uri);
}
});
}
});
}else {
sendResult(res,0,null,'上传失败!文件为空');
}
});
module.exports = router;
sendResult是给前台返回值的function,因为我这里存放的文件夹是前台传值来查询SQL来确定的,所以,我这里文件上传还查询了一句SQL。同时,这里文件上传到服务器上,因为这里是通过前台http上传直接传输到这里文件路径下,上传上的文件所有的权限是只有W和R,所以这里执行了一个脚本,来更改上传文件的权限。
FTP
没有办法直接使用浏览器上传FTP,如果FTP服务和server服务不在一台机器上的时候,我们只有通过前台上传文件到后台server,然后在将文件上传到ftp,然后删除server上的临时文件,来完成此功能
var express = require('express');
var router = express.Router();
var fs = require('fs');
var path = require('path');
var fstool = require('../utils/fsTool.js');
var moment = require('moment');
var dbcon = require("./mysqlconn.js");
var _ = require("lodash");
var JSFtp = require("jsftp");
var multiparty = require('connect-multiparty')({
uploadDir: process.env.TMPDIR
});
fstool.clearDir(process.env.TMPDIR);
function uploadFiles(req) {
// console.log(req.files,req.body);
var f = req.files.file || {};
var pt = require('path').relative(process.env.WEBROOT, f.path || "");
return {
url: pt,
file:f
};
}
function sendResult(res,status,data,msg){
var d = {status:status};
if(data) d.data = data;
if(msg) d.msg = d.error = msg;
res.status(status?200:500).send(d);
}
function getScriptPath() {
return process.env.ROOT + "/externaltools/";
}
router.post('/special', multiparty, function(req, res) {
var file = uploadFiles(req);
if (file && file.url){
// var ext = path.extname(file.url)
// var timestamp = moment().format("YYYYMMDDHHmm");
var fname = req.files.file.originalFilename;
var uri = "store/temp/"+fname;
var targetPath = process.env.WEBROOT+uri;
console.log(targetPath);
var sqlstr = "SELECT * FROM t_user WHERE nId='" + req.session.userId + "'";
dbcon(sqlstr, function(err, vals, fields) {
if (err) {
sendResult(res,0,null,'上传失败!数据库查询错误');
console.log(err);
} else {
// fstool.mkDirs(targetPath);
fstool.moveFile(file.file.path,targetPath,function(){
var Ftp = new JSFtp({
host: "10.42.10.34",
user: vals[0].strUserId,
pass: vals[0].strFtpPassword
});
var url = './'+req.query.id+'/'+fname;
Ftp.put(targetPath, url, function(hadError) {
if (!hadError){
console.log("File transferred successfully!");
fs.unlink(targetPath,function(error){
if(error){
console.log('删除失败');
sendResult(res,0,null,'上传失败!移除临时文件失败');
}else{
console.log('删除成功');
sendResult(res,1,uri);
}
});
}else{
console.log(hadError);
sendResult(res,0,null,'上传失败!ftp上传失败');
}
});
});
}
});
}else {
sendResult(res,0,null,'上传失败!文件为空');
}
});
这里文件从后台server上传到ftp这里使用了node的jsFTP模块,来完成这个功能,先将文件通过浏览器走http上传到后台的临时文件夹,然后ftp上传,最后删除临时文件,整个上传结束。
网友评论