uploaddirect.js
11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
//评论管理
angular.module("xn/template/uploadDirect/uploadFile.html", [])
.run(["$templateCache", function($templateCache) {
"use strict";
$templateCache.put("xn/template/uploadDirect/uploadFile.html",
"<div class='clearfix'>" +
" <div class='clearfix'>" +
" <button id=\"{{browseButton}}\" class=\"btn btn-default mr_15 f-right\" name=\"pickfiles\" >增加</button>" +
" <button class=\"btn btn-primary mr_15 f-right\" ng-click=\"upClick()\" ng-show=\"upFiles.length > 0\">上传</button>" +
" </div>"+
" <div class='mt_12 clearfix' id='{{container}}'>" +
" <div ng-if='vm.isShow' class=\"xn-table-wrap clearfix \" id=\"container\" >"+
" <table class=\"table table-bordered\">"+
" <thead>"+
" <tr>"+
" <th class=\"xn-text-center xn-col-sm-8\">文件名</th>"+
" <th class=\"xn-text-center xn-col-sm-3 xn-center\">格式</th>"+
" <th class=\"xn-text-center xn-col-sm-6 xn-center\">文件大小</th>"+
" <th class=\"xn-text-center xn-col-sm-10 xn-center\">状态</th>"+
" </tr>"+
" </thead>"+
" <tbody id=\"tbody\">"+
" <tr ng-repeat=\"file in upFiles\">"+
" <td>{{file.name}}</td>"+
" <td class=\"xn-center\">{{file.fileType}}</td>"+
" <td class=\"xn-center\">{{file.size | fileSize}}</td>"+
" <td class=\"xn-text-center\" ng-show=\"file.percent\">"+
" {{file.status | fileStatusType}}【{{file.percent}}%】"+
" </td>"+
" <td class=\"xn-text-center\" ng-show=\"!file.percent\">"+
" {{file.status | fileStatusType}}"+
" </td>"+
" </tr>"+
" </tbody>"+
" </table>"+
" </div>"+
" </div>"+
"<div/>"
);
}]);
angular.module("uploadDirect",["xn/template/uploadDirect/uploadFile.html"])
.directive("xnUploadDirect",["uploadDirectService","dialogService","$timeout",function(uploadDirectService,dialogService,$timeout) {
return {
restrict: "AE",
scope: {
upFiles: "=ngModel",
url: "@",
path: "@",
method:"&",
isShow:"@"
},
replace: true,
require: "?ngModel",
templateUrl: "xn/template/uploadDirect/uploadFile.html",
link: function(scope,element,atter,ngModel) {
scope.vm={
isShow:true
};
if(scope.isShow=="false"){
scope.vm.isShow=false;
}
scope.upFiles=[];
var timeStamp=new Date().getTime();
scope.browseButton="browseButton"+timeStamp;
scope.container="container"+timeStamp;
$timeout(function () {
// 初始化上传
var uploader = new plupload.Uploader({
runtimes : 'html5,flash,silverlight,html4',
browse_button:scope.browseButton,
container:scope.container,
prevent_duplicates:true,//阻止重复文件上传
flash_swf_url : scope.path||'https://cdn.xiniunet.com/js/plupload/2.1.2/js/Moxie.swf',
silverlight_xap_url :scope.path||'https://cdn.xiniunet.com/js/plupload/2.1.2/js/Moxie.xap',
url : scope.url||'http://oss.aliyuncs.com',
init: {
PostInit: function() {
},
//添加文件
FilesAdded: function(up, files) {
for(var j=0; j<files.length; j++) {
var file = files[j];
var item = {
fileId : file.id,
name : file.name,
fileType :file.name.substring(file.name.lastIndexOf(".") + 1, file.name.length),
status : file.status,
percent : file.percent,
size : file.size
};
scope.upFiles.push(item);
}
scope.$apply();
scope.upClick=function () {
if(scope.upFiles.length>0) {
set_upload_param(uploader, '', false);
console.log(scope.upFiles);
}
}
},
//上传前
BeforeUpload: function(up, file) {
set_upload_param(up, file.name, true);
},
//上传中
UploadProgress: function(up, file) {
var item = {};
for (var i = 0; i < scope.upFiles.length; i++) {
var obj = scope.upFiles[i];
if (file.id == obj.fileId) {
item = scope.upFiles[i];
break;
}
}
item.status = file.status;
item.percent = file.percent;
scope.$apply();
},
//上传结束
FileUploaded: function(up, file, info) {
var item = {};
for (var i = 0; i < scope.upFiles.length; i++) {
var obj = scope.upFiles[i];
if (file.id == obj.fileId) {
item = scope.upFiles[i];
break;
}
}
item.status = file.status;
item.percent = file.percent;
if (file.status == "5") {
item.storagePath=returnObj.host + "/" + returnObj.dir + file.name;
item.extension = item.fileType;
item.sourceType = "DIRECT";//直传
uploadDirectService.createFile(item).success(function (result) {
if (result.errors === null && result.errors.length > 0) {
dialogService.tip(result.errors);
} else {
item.id = result.id;
}
});
}
scope.$apply();
},
//获取异常
Error: function(up, err) {
console.log(err);
}
}
});
// 获取签名
uploadDirectService.getMyUploadFile().success(function(data){
returnObj=data;
g_dirname = data.dir;
uploader.init();
});
},0);
var g_object_name = '';
var returnObj;
var g_dirname;
function set_upload_param(up, filename, ret) {
g_object_name = g_dirname;
if (filename != '') {
calculate_object_name(filename)
}
var new_multipart_params = {
'key' : g_object_name,
'policy': returnObj.policy,
'OSSAccessKeyId': returnObj.accessid,
'success_action_status' : '200', //让服务端返回200,不然,默认会返回204
'signature': returnObj.signature
};
up.setOption({
'url': returnObj.host,
'multipart_params': new_multipart_params
});
up.start();
}
function calculate_object_name(filename) {
g_object_name += "${filename}";
return ''
}
}
};
}])
.filter("fileStatusType",[
function(){
return function(input){
switch(input){
case 1:
input = "队列中";
break;
case 2:
input ="上传中";
break;
case 4:
input ="上传失败";
break;
case 5:
input ="上传完成";
break;
default :
break;
}
return input;
}
}
])
.filter("fileSize", [
function() {
return function (size) {
if(size < 1) {
return "0B";
} else if(size < 1024) {
return size + "B";
} else if(size < 1000 * 1000) {
return Math.round(size / 1024 * 10) / 10.0 + "KB";
} else if(size < 1000 * 1000 * 1000) {
return Math.round(size / 1024 / 1024 * 10) / 10.0 + "MB";
} else {
return Math.round(size / 1024 / 1024 / 1024 * 10) / 10.0 + "GB";
}
}
}
])
.factory('uploadDirectService',['$http',function($http){
var service={};
service.getMyUploadFile=function(request) {
var url=xnConfig.myUrl+"api/ossSign.do";
return $http({
method : "get",
// withCredentials:true,
url : url
//data:request
});
};
service.createFile=function(request) {
var url=xnConfig.myUrl+"api/foundation.do";
return $http({
method : 'POST',
url : url,
withCredentials:true,
params:{"method":"api.foundation.file.create","jsonParam":request}
//data:request
});
};
return service;
}]);