How-to-upload-files-using-HTML5

How to upload files using HTML5

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
/**
* Created by oksite on 2017/7/3.
*/
module.exports = function() {
/*init --> date*/
this.defaults = {
targetDiv : null,
taskId : '',
imgCount : 0,
affixCount : 0,
fileType : 1,
browseBtn : 'fileToUpload',
fileSize : '100mb',
thisUploadUrl : '/mobile/upload',
id : 'newid',
maxCount : 10,
successCallback :null
};
/*save --> date*/
this.opts = {};
/*export --> api*/
this.upload =function (options) {
var that = this;
this.opts = $.extend(that.defaults,options);
this.opts.targetDiv = that.opts.browseBtn.parent().parent().parent();
this.opts.browseBtn.change(function() {
/* Act on the event */
var file = $(this)[0].files[0];
/*if size >100mb*/
if(file.size > 100*1024*1024){
dialog({id:'ui-dialog-prompt',timeout:2000,content:'<i class="ui-dialog-icon-error"></i>上传文件不能大于100kb!'}).show();
}
/*ser FormData send server*/
var formData = new FormData();
formData.append("file", file);
formData.append("s", 'web');
formData.append("uid", gtUtil.cookieUtil.getCookie('userid'));
formData.append("sid", gtUtil.cookieUtil.getCookie('sessionID'));
formData.append("sessionID", gtUtil.cookieUtil.getCookie('sessionID'));
formData.append("taskId",that.opts.taskId);
formData.append("fileType",that.opts.fileType);
formData.append("moduleType","0");
/*ser xhr send server*/
var xhr = new XMLHttpRequest();
/*progress*/
xhr.upload.addEventListener("progress", function(evt) {
if (evt.lengthComputable) {
var percentComplete = Math.round(evt.loaded * 100 / evt.total) + '%';
var $cancase= $('<div class="cancase"><div class="progressbar"><div class="text">'+percentComplete+'</div><div class="bar" style="width: '+percentComplete+';"></div></div><bdo></bdo></div>')
that.opts.targetDiv.find('.myFileList ul').append($cancase);
that.opts.targetDiv.find('.myFileList ul .cancase bdo').click(function () {
dialog({id:'ui-dialog-prompt',timeout:2000,content:'<i class="ui-dialog-icon-error"></i>您取消了文件上传!'}).show();
xhr.abort();
});
} else {
that.opts.targetDiv.find('.myFileList ul').append('<li class="loading" id="li_' + that.opts.id + '">unable to compute</li>');
}
}, false);
/*load*/
xhr.addEventListener("load", function(res) {
that.opts.targetDiv.find('.myFileList ul').find('div.cancase').remove();
var data = JSON.parse(res.target.responseText);
if(data.responseData.succeed == true){
var _html = that.buildFileHtml(data.responseData.jtFile);
$('<li id="'+that.opts.id+'">').appendTo(that.opts.targetDiv.find('.myFileList ul')).html(_html)
that.opts.targetDiv.find('.myFileList ul li span.myadwrong').click(function(){
that.delfilelist($(this));
});
}else{
dialog({id:'ui-dialog-prompt',timeout:2000,content:'<i class="ui-dialog-icon-error"></i>系统忙,请稍后再试!'}).show();
if (that.opts.id.indexOf('newid') >= 0) that.opts.targetDiv.find(".myFileList #li_" + that.opts.id).remove();
else that.opts.targetDiv.find(".myFileList #li_" + that.opts.id).html(that.htmls);
}
}, false);
/*error*/
xhr.addEventListener("error", function (res) {
dialog({id:'ui-dialog-prompt',timeout:2000,content:'<i class="ui-dialog-icon-error"></i>上传失败,请刷新后重试!'}).show();
}, false);
/*abort*/
xhr.addEventListener("abort", function(res) {
dialog({id:'ui-dialog-prompt',timeout:2000,content:'<i class="ui-dialog-icon-error"></i>您取消了本次上传!'}).show();
}, false);
/*server*/
xhr.open("POST", "/mobile/upload");
xhr.setRequestHeader("s", 'web');
xhr.setRequestHeader("sessionID", gtUtil.cookieUtil.getCookie('sessionID'));
/*end*/
xhr.send(formData);
});
};
/*build --> html*/
this.buildFileHtml=function(data) {
var tempData = {};
var fileSize = parseInt(data.fileSize, 10);
if (fileSize < 1000){
tempData.fileSize = fileSize + "B";
} else if (fileSize < 1000000){
tempData.fileSize = (Math.round(100 * fileSize / 1000) / 100) + "K";
}else {
tempData.fileSize = (Math.round(100 * fileSize / 1000000) / 100) + "M";
}
tempData.fileUrl = GT_CONSTANT.nginxRoot + '/mobile/download?id=' + data.id;
tempData.fileName = data.fileTitle == null ? data.fileName : data.fileTitle;
tempData.suffixName = 'public';
var tpl = Handlebars.compile(__inline('file.handlebars'));
var ret = tpl(tempData);
return ret;
};
/*get id --> get filelist*/
this.getFileList=function(){
var that = this;
if(that.opts.taskId){
var d = {
"taskId": that.opts.taskId
};
$.ajax({
beforeSend: function(request) {
request.setRequestHeader('s', 'web');
request.setRequestHeader('sessionID', gtUtil.cookieUtil.getCookie('sessionID'));
},
url: GT_CONSTANT.dataPath + "/demand/findDemandFile.json",
type: 'POST',
dataType: 'json',
async: true,
data: JSON.stringify(d),
success: function(result2) {
if (result2.responseData.succeed) {
attFileList = result2.responseData.demandFileVO.attFile.fileList;
pvFileList = result2.responseData.demandFileVO.pvFile.fileList;
if (attFileList.length > 0) {//附件(全部显示)
$.each(attFileList,function(i,o){
var html = that.buildFileHtml(o);
that.opts.targetDiv.find('.myFileList ul').append('<li id="li_'+o.id+'">'+html+'</li>');
});
that.opts.targetDiv.find('.myFileList ul li span.myadwrong').click(function(){
that.delfilelist($(this));
});
}
}
}
});
}
};
/*del --> filelist*/
this.delfilelist=function(el){
dialog({
id : 'ui-dialog-prompt',
title : '删除确认',
content : '<i class="ui-dialog-icon-warn"></i>您确定要删除吗?',
ok : (function() {
var d = {
"id": el.attr("id").substring(6)
};
$.ajax({
beforeSend: function(request) {
request.setRequestHeader('s', 'web');
request.setRequestHeader('sessionID', gtUtil.cookieUtil.getCookie('sessionID'));
},
url: GT_CONSTANT.dataPath + "/demand/deleteFile.json",
type: 'POST',
dataType: 'json',
async: true,
data: JSON.stringify(d),
success: function(result) {
if(result.responseData){
el.parent().remove();
}else{
dialog({id:'ui-dialog-prompt',timeout:2000,content:'<i class="ui-dialog-icon-error"></i>删除失败,请刷新后重试!'}).show();
}
},
error:function (res) {
dialog({id:'ui-dialog-prompt',timeout:2000,content:'<i class="ui-dialog-icon-error"></i>删除失败,请刷新后重试!'}).show();
}
});
}),
cancel : function(){}
}).show();
};
}