express config:
server.configure(function () {
server.use(express.bodyParser({
keepExtensions:true,
limit:100000000,
defer:true,
uploadDir: "d:/"})
);
上传控制器:
exports.upload = function(req, res){
req.form.on('progress',function(bytesReceived, bytesExpected){
res.send({loaded: bytesReceived, total: bytesExpected});
});
req.form.on('complete',function(){
if(!path.existsSync(uploadDir)){
mkdirp(uploadDir, function (err) {
if (err) console.error(err)
saveImg(req, res);
});
} else {
saveImg(req, res);
}
});
};
var saveImg = function(req, res) {
var scrName = req.files.files[0].path;
var distName = uploadDir + uuid.v4() + '.png';
var gm = require('gm');
gm(scrName)
.resize(1000, 1000)
.autoOrient()
.write(distName, function (err) {
fs.unlink(scrName);
// res.send({distName: distName});
});
}
虽然我没有调用“res.send({distName:distName})”,但是当上传成功时客户端会得到响应,那么如何在jquery代码中获取新的图像名称如下所示:
$('#fileupload').fileupload({
url: Vari.ApiPath + 'upload',
dataType: 'json',
done: function (e, data) {
console.log(data.distName);
},
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#progress .progress-bar').css(
'width',
progress + '%'
);
}
}).prop('disabled', !$.support.fileInput)
.parent().addClass($.support.fileInput ? undefined : 'disabled');