base64轉換mp3檔案,在伺服器端(nodejs)反編碼命名儲存
/**
* Created with JetBrains WebStorm.
* User: vasdaq
* Date: 13-12-30
* Time: 下午4:36
* To change this template use File | Settings | File Templates.
*/
var fs = require('fs');
var model=require('../model/upldModel.js'); //model模組
var util=require('../model/util.js');
var EventProxy =require('eventproxy').EventProxy;
var i18n = require('i18n');
var querystring = require('querystring');
exports.upld = function(req, res){
i18n.setLocale(req.language);
var method = req.method.toLowerCase();
var videoID={videoID:req.body.vdoID};
var mp3Data={mp3Data:req.body.mp3Data};
var voiceName={voiceName:req.body.voiceName};
var voiceAuthor={voiceAuthor:req.body.voiceAuthor};
mp3Data = querystring.unescape(mp3Data.mp3Data);
var base64_Data = mp3Data.replace(/^data:audio\/\w+;base64,/,"");
var dataBuffer = new Buffer(base64_Data, 'base64');
var now =Date.now();//獲取系統當前時間數值
var savePath = './public/audio/' +videoID.videoID+'_'+voiceAuthor.voiceAuthor+'_'+now+'.mp3';//伺服器儲存檔名
var voicePath = '/audio/' +videoID.videoID+'_'+voiceAuthor.voiceAuthor+'_'+now+'.mp3';//資料庫儲存名
fs.writeFile(savePath,dataBuffer,function(err){
if(err){
console.log(err);
// res.send({tip:'上傳失敗,請再次嘗試',backUrl:'/record.html'});
}else{
}
});
// console.log(videoID);
// console.log(mp3Data);
// console.log(voicePath);
var proxy = new EventProxy();
var up = function(result){
// console.log(result.bl);
if(method === "post"){
res.send({result:result.bl,backUrl:'/record.html'});
}
};
proxy.assign("result",up);
model.upldBind(voiceName.voiceName,videoID.videoID,voicePath,voiceAuthor.voiceAuthor,function(result){
proxy.trigger("result",result);
});
};