<div style = "display: none">
<form id = "csp_oper_manage_load_oper_form" action="index.html" method="post">
<input type="file" id = "csp_oper_manage_load_oper_file_input" onchange = "cspOperManageLoadOperFileChange(this)">
<button type="button" id = "csp_oper_manage_load_oper_error_btn" ng-click = "cspOperManageLoadOperFileError()">错误</button>
<button type="button" id = "csp_oper_manage_load_oper_file_load_btn" ng-click = "cspOperManageLoadOperFileLoad()">上传</button>
</form>
</div>
$scope.cspOperManageLoadOperFileLoad = function() {
$scope.loadoperinfo.errorString = "正在上传并解析文件";
var fd = new FormData();
var file = document.getElementById('csp_oper_manage_load_oper_file_input').files[0];
fd.append('file', file);
var second =5;
operManageService.cspOperManageLoadOperUploadFile(fd,function(fdres) {
if(fdres.result == "succeed") {
$scope.loadopers.splice(0,$scope.loadopers.length);
$scope.loadopers = fdres.data;
$scope.loadoperinfo.loading = false;
$scope.loadoperinfo.fileright = true;
for(var i = 0; i < $scope.loadopers.length; i++) {
$scope.loadopers[i].selected = false;
}
}
else {
$scope.loadoperinfo.error = true;
$scope.loadoperinfo.errorString = fdres.reason;
var timePromise = undefined;
timePromise = $interval(function(){
if(second<=0){
$interval.cancel(timePromise);
timePromise = undefined;
$scope.loadoperinfo.loading = false;
}
else{
second--;
}
},500,100);
}
})
}
}
service.cspOperManageLoadOperUploadFile = function(params,callback) {
$http({
method:'POST',
url:"/opermanage/loadoperuploadfile",
data: params,
headers: {'Content-Type':undefined},
transformRequest: angular.identity
}).success(function(res) {
callback(res);
}).error(function(res) {
var ret = {};
ret.result = "failed";
ret.reason = "请求失败";
callback(ret);
});
}
router.post('/loadoperuploadfile',function(req,res) {
authorization.checkOperAuthorization(req,function(result) {
if(result.authorization == true) {
var form = new multiparty.Form({uploadDir: './app/temp/'});
form.parse(req, function(err, fields, files) {
var filesTmp = JSON.stringify(files,null,2);
if(err) {
res.send({"result":"failed","reason":"文件上传失败"});
res.end();
}
else {
var inputFile = files.file[0];
var uploadedPath = inputFile.path;
var filekindarr = uploadedPath.split(".");
var kind = filekindarr[filekindarr.length -1].toLowerCase();
if(kind == "csv") {
try {
var fileStr = fs.readFileSync(uploadedPath, {encoding:'binary'});
var buf = new Buffer(fileStr, 'binary');
var str = iconv.decode(buf, 'GBK');
} catch (e) {
if(fs.existsSync(uploadedPath)) {
fs.unlinkSync(uploadedPath);
}
console.error(e);
res.send({"result":"failed","reason":"文件内容格式错误"});
res.end();
} finally {
var rows =str.split("\r\n");
for(var j=0;j<rows.length;j++){
if(rows[j]=='')
rows.splice(j,1);
}
if(rows.length > 1) {
var cvsData = []
for(var k = 0; k < rows.length; k ++) {
var rData = [];
rData = rows[k].split(",");
cvsData[k] = rData;
}
if(cvsData[0][0] === "资产管理人编号" && cvsData[0][1] === "操作员账号" && cvsData[0][2] === "操作员名称" && cvsData[0][3] === "手机号码") {
var cvsjson = [];
for(var m = 1; m < cvsData.length; m ++) {
var oper = {};
oper.maid = cvsData[m][0];
oper.oid = cvsData[m][1];
oper.oname = cvsData[m][2];
oper.phone = cvsData[m][3];
cvsjson[m-1] = oper;
}
res.send({"result":"succeed","data":cvsjson});
res.end();
}
else {
res.send({"result":"failed","reason":"文件内容格式错误"});
res.end();
}
}
else {
res.send({"result":"failed","reason":"文件内容格式错误"});
res.end();
}
if(fs.existsSync(uploadedPath)) {
fs.unlinkSync(uploadedPath);
}
}
}
else {
try {
var obj = xlsx.parse(uploadedPath);
} catch (e) {
if(fs.existsSync(uploadedPath)) {
fs.unlinkSync(uploadedPath);
}
console.error(e);
res.send({"result":"failed","reason":"文件内容格式错误"});
res.end();
} finally {
if(typeof(obj) == "undefined") {
return;
}
var xlsData = obj[0].data;
if(xlsData.length > 1) {
if(xlsData[0][0] === "资产管理人编号" && xlsData[0][1] === "操作员账号" && xlsData[0][2] === "操作员名称" && xlsData[0][3] === "手机号码") {
var xlsjson = [];
for(var i = 1; i < xlsData.length; i ++) {
var oper = {};
oper.maid = xlsData[i][0];
oper.oid = xlsData[i][1];
oper.oname = xlsData[i][2];
oper.phone = xlsData[i][3];
xlsjson[i-1] = oper;
}
res.send({"result":"succeed","data":xlsjson});
res.end();
}
else {
res.send({"result":"failed","reason":"文件内容格式错误"});
res.end();
}
}
else {
res.send({"result":"failed","reason":"文件内容格式错误"});
res.end();
}
if(fs.existsSync(uploadedPath)) {
fs.unlinkSync(uploadedPath);
}
}
}
}
})
}
else {
res.send({"result":"failed","reason":"未授权的请求"});
res.end();
}
})
})