Authored by xuhongyun

支持多文件上传

@@ -130,7 +130,25 @@ module.exports={ @@ -130,7 +130,25 @@ module.exports={
130 result={code: "501",message: "用户失效,请重新登录"}; 130 result={code: "501",message: "用户失效,请重新登录"};
131 return callback(null, result); 131 return callback(null, result);
132 } 132 }
133 - req.body.files = [fs.createReadStream(req.files.upfile.path)]; 133 +
  134 + var files = req.files.upfile;
  135 + console.log(req.files);
  136 + //判断是单张传还是多张传
  137 + var flag = Object.prototype.toString.call(files) === '[object Array]';
  138 + //如果是单张,则数组化
  139 + if (!flag) {
  140 + files = [];
  141 + files.push(req.files.upfile);
  142 + }
  143 +
  144 + req.body.files = [];
  145 + req.body.fileNames = [];
  146 + for (var index = 0; index < files.length; index++) {
  147 + req.body.files[index] = fs.createReadStream(files[index].path);
  148 + req.body.fileNames[index] = files[index].filename;
  149 + }
  150 +
  151 + // req.body.files = [fs.createReadStream(req.files.upfile.path)];
134 req.body.bucket="goodsimg"; 152 req.body.bucket="goodsimg";
135 request.post({ 153 request.post({
136 url: "http://upload.static.yohobuy.com", 154 url: "http://upload.static.yohobuy.com",
@@ -142,21 +160,27 @@ module.exports={ @@ -142,21 +160,27 @@ module.exports={
142 if (!error && httpResponse.statusCode == 200) { 160 if (!error && httpResponse.statusCode == 200) {
143 var ret = JSON.parse(body); 161 var ret = JSON.parse(body);
144 if (ret.code === 200) { 162 if (ret.code === 200) {
145 - var imgUrl = ret.data.imagesList[0];  
146 - imgUrl=Utils.joinImg(imgUrl);  
147 - result={}; 163 + result = [];
  164 + for (var i = 0; i < ret.data.imagesList.length; i++) {
  165 + var imgUrl = ret.data.imagesList[i];
  166 + var rest = {};
  167 + imgUrl = Utils.joinImg(imgUrl);
  168 +
148 if(imgUrl){ 169 if(imgUrl){
149 - result.originalName=Date.now();  
150 - result.name=Date.now();  
151 - result.url=imgUrl;  
152 - result.type=imgUrl.substring(imgUrl.lastIndexOf('.'));  
153 - result.state="SUCCESS"; 170 + rest.originalName = Date.now();
  171 + rest.name = Date.now();
  172 + rest.url = imgUrl;
  173 + rest.type = imgUrl.substring(imgUrl.lastIndexOf('.'));
  174 + rest.state = "SUCCESS";
  175 +
154 }else{ 176 }else{
155 - result.state="FAIL"; 177 + rest.state = "FAIL";
156 } 178 }
  179 + result.push(rest);
157 } 180 }
158 } 181 }
159 return callback(null, result); 182 return callback(null, result);
  183 + }
160 }); 184 });
161 }, 185 },
162 downExcel:function(req, callback){ 186 downExcel:function(req, callback){
@@ -137,7 +137,7 @@ var $=require('jquery'); @@ -137,7 +137,7 @@ var $=require('jquery');
137 '<div class="edui-image-upload%%">' + 137 '<div class="edui-image-upload%%">' +
138 '<span class="edui-image-icon"></span>' + 138 '<span class="edui-image-icon"></span>' +
139 '<form class="edui-image-form" method="post" enctype="multipart/form-data" target="up">' + 139 '<form class="edui-image-form" method="post" enctype="multipart/form-data" target="up">' +
140 - '<input style=\"filter: alpha(opacity=0);\" class="edui-image-file" type="file" hidefocus name="upfile" accept="image/gif,image/jpeg,image/png,image/jpg,image/bmp"/>' + 140 + '<input style=\"filter: alpha(opacity=0);\" class="edui-image-file" type="file" multiple hidefocus name="upfile" accept="image/gif,image/jpeg,image/png,image/jpg,image/bmp"/>' +
141 '</form>' + 141 '</form>' +
142 142
143 '</div>', 143 '</div>',
@@ -182,9 +182,12 @@ var $=require('jquery'); @@ -182,9 +182,12 @@ var $=require('jquery');
182 uploadComplete: function(r){ 182 uploadComplete: function(r){
183 var me = this; 183 var me = this;
184 try{ 184 try{
185 - r=r.match(/\{.*\}/g)[0]; 185 + r=r.match(/\[.*\]/g)[0];
186 var json = eval('('+r+')'); 186 var json = eval('('+r+')');
187 - Base.callback(me.editor, me.dialog, json.url, json.state); 187 + for (var i = 0; i < json.length; i++) {
  188 + Base.callback(me.editor, me.dialog, json[i].url, json[i].state);
  189 + }
  190 +
188 }catch (e){ 191 }catch (e){
189 var lang = me.editor.getLang('image'); 192 var lang = me.editor.getLang('image');
190 Base.callback(me.editor, me.dialog, '', (lang && lang.uploadError) || 'Error!'); 193 Base.callback(me.editor, me.dialog, '', (lang && lang.uploadError) || 'Error!');
@@ -193,7 +196,7 @@ var $=require('jquery'); @@ -193,7 +196,7 @@ var $=require('jquery');
193 submit: function (callback) { 196 submit: function (callback) {
194 197
195 var me = this, 198 var me = this,
196 - input = $( '<input style="filter: alpha(opacity=0);" class="edui-image-file" type="file" hidefocus="" name="upfile" accept="image/gif,image/jpeg,image/png,image/jpg,image/bmp">'), 199 + input = $( '<input style="filter: alpha(opacity=0);" class="edui-image-file" type="file" multiple hidefocus="" name="upfile" accept="image/gif,image/jpeg,image/png,image/jpg,image/bmp">'),
197 input = input[0]; 200 input = input[0];
198 201
199 $(me.dialog).delegate( ".edui-image-file", "change", function ( e ) { 202 $(me.dialog).delegate( ".edui-image-file", "change", function ( e ) {
@@ -405,8 +408,13 @@ var $=require('jquery'); @@ -405,8 +408,13 @@ var $=require('jquery');
405 } 408 }
406 }); 409 });
407 $(document).on("click",".edui-image-product .edui-product-content .edui-image-pic",function(){ 410 $(document).on("click",".edui-image-product .edui-product-content .edui-image-pic",function(){
408 - $(".edui-image-product .edui-product-content .edui-image-pic").removeClass("active-product");  
409 - $(this).addClass("active-product"); 411 + if ($(this).hasClass("active-product")) {
  412 + $(this).removeClass("active-product");
  413 + }
  414 + else {
  415 + $(this).addClass("active-product");
  416 + }
  417 +
410 }); 418 });
411 } 419 }
412 } 420 }