在后端我发送:res.send(JSON.stringify(back));
back
,在字符串化之后是:
{"success":true,"data":{"success":true,"data":{"name":"-kkWjmTLb","version":1,"dateCreated":1407448946239,"dateUpdated":1407448946239,"vidLoc":"rtmp://xxxxxxx.cloudfront.net/cfx/st/vid_-kkWjmTLb","thumbLoc":"https://xxxxxxxxxxx.cloudfront.net/big_icon.png","author":"53a47a469c52c9d83a2d71d9","videoBucket":"xx.xxxxxxxx","thumbBucket":"xx.xxxxxxxx","scenario":false,"fakeName":xxxxxxxxxx.flv"}}}
`
在前端,我这样做:
$(htmlId).uploadify('settings','onUploadSuccess', function(file, data, res) {
console.log('uploadSuccess',data);
控制台日志告诉我data
是OK
那里没有那么大的字符串,只是OK
。
如何将我的字符串作为对uploadify的响应来获取前端?
完整的Bakend代码:
app.post('/uploadVideo',function(req,res){
console.log("hit video upload");
checkProperties(req.query,['name','ext','tenant'],function(results){
if(!results.error){
var form = new formidable.IncomingForm();
form.uploadDir = __dirname+'/uploads';
form.keepExtensions = true;
console.log('queries are ',req.query);
form.on('file',function(field,file){
var data = {};
var uid = shortId.generate();
var renamed = form.uploadDir + "/vid_"+uid;
fs.rename(file.path,renamed);
if(typeof req.query.tenant !== 'undefined'){
console.log('setting data');
data.tenant = req.query.tenant;
data.fakeName = req.query.name;
data.name = uid;
console.log('data set',data);
}else{
console.log('no tenant');
res.send(400);
return;
}
console.log('calling uploadVideoS3',data);
uploadVideoS3(renamed,req.query.ext,data,fs.readFileSync(renamed),function(back){
if(back.success){
console.log('done uploaded to S3',JSON.stringify(back));
res.send(JSON.stringify(back));
}else{
console.log('video upload S3 failed');
res.send(400);
}
});
});
form.on('error',function(err){
console.log("error with file upload " + err);
res.send(400);
res.end();
});
form.on('end',function(){
console.log("done");
});
form.parse(req, function(err, fields, files) {
res.send(200);
res.end();
});
}else{
res.send(400);
res.send(results.message);
}
});
});
前端代码:
$scope.initVideoUploader = function(){
uploadifyService.videoUploadInit('#videoUpload',function(back){
if(back.data){
console.log('file selected is:',back.data.name);
var name = back.data.name;
var ext = back.data.name.substring(back.data.name.lastIndexOf('.')+1);
uploadifyService.uploadVideo('#videoUpload',name,ext);
}
if(back.identifier){
console.log(back);
// var item = _.findWhere($scope.content.myContent.videos, {name: recorderInfo.id.toString()});
// console.log(recorderInfo.id, item);
// $scope.content.openItem(item);
}
});
};
uploadifyService.js:
videoUploadInit: function(htmlId,callback){
$(htmlId).uploadify({
'scriptAccess': 'always',
'multi': false,
'auto': true,
'swf': '/js/plugins/uploadify.swf',
'buttonText': 'Upload Video',
'fileTypeExts': '*.flv;',
'fileSizeLimit': '20MB',
'onSelect': function(file, queueId) {
console.log('videoUploadInit onSelect hit');
callback({
data: file,
queueId: queueId
});
},
'onUploadSuccess': function(file, data, res) {
console.log('upload success from videoUploadInit',res);
callback({
identifier:true,
data2:data,
response:res
});
},
});
},
uploadVideo:function(htmlId,name,ext){
$(htmlId).uploadify('settings','uploader',configService.getNodeSocketApi() + "/uploadVideo?tenant=" + sessionService.getCustomerId() + sessionService.getUserId()+'&ext='+ext+'&name='+name);
$(htmlId).uploadify('settings','script',configService.getNodeSocketApi() + "/uploadVideo?tenant=" + sessionService.getCustomerId() + sessionService.getUserId()+'&ext='+ext+'&name='+name);
$(htmlId).uploadify('upload', '*');
},
答案 0 :(得分:0)
因为我在后端有这个:
form.parse(req, function(err, fields, files) {
res.send(200);
res.end();
});
在我打算之前发送了回复。