-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupload.html
345 lines (276 loc) · 7.72 KB
/
upload.html
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="format-detection" content="telephone=no"/>
<title>图片上传</title>
<style>
body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td,hr,button,article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section {
margin:0;padding:0;
}
body{
font: 14px/1.5 tahoma,"microsoft yahei","微软雅黑E\8F6F\96C5\9ED1";
}
header {
height:40px;
line-height:40px;
padding-left:10px;
background: -webkit-gradient(linear, 0 0, 0 100%, from(#fefefe), to(#e8e8e8));
}
footer{
position:absolute;
bottom:0px;
padding:10px;
}
.submitbtn{
display: inline-block;
min-width: 60px;
height: 30px;
padding: 0 15px;
border: none;
background: #ff4400;
text-align: center;
text-decoration: none;
line-height: 30px;
color: #ffffff;
font-size: 14px;
font-weight: 700;
-webkit-border-radius: 2px;
background: -webkit-gradient(linear, 0 0, 0 100%, color-stop(0, #ff5500), color-stop(1, #ff4400));
text-shadow: 0 -1px 1px #ca3511;
-webkit-box-shadow: 0 -1px 0 #bf3210 inset;
}
header h1{
font-size:16px;
}
#content{
padding:10px;
}
.imglist {
float:left;
margin:10px 10px 10px 0;
width:60px;
text-align:center;
}
.imglist .plus, .imglist .img, .imglist .img img {
width:60px;
height:60px;
overflow:hidden;
}
.imglist .plus{
color:#eee;
font:72px/60px Arial;
border:2px dashed #eee;
box-sizing: border-box
}
.imglist .img img{
display:block;
overflow:hidden;
}
.imglist .btn{
color:#35a1d4;
}
input[type="file"]{
/*不能直接设置display:none,某些浏览器上由于安全限制,不能处罚click事件*/
/*display:none;*/
position:absolute;
left:-1000px;
}
#content:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
.describe{
clear:both;
padding-top:20px;
}
.describe textarea{
width:95%;
line-height: 20px;
padding: 4px 6px;
font-size: 12px;
color: #595959;
resize: auto;
cursor: auto;
margin-bottom:10px;
}
#loading{
display:none;
margin:10px;
}
#loading span{
display: inline-block;
-webkit-animation: spin 2s infinite linear;
-moz-animation: spin 2s infinite linear;
animation: spin 2s infinite linear;
}
#message{
display:none;
margin:10px;
padding:10px;
width: 190px;
max-width: 400px;
background: #f7ab06;
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #ffba27), color-stop(100%, #f7ab06));
border: 1px solid #e68200;
border-radius: 3px;
-webkit-box-shadow: inset 0px 0px 2px 2px #ffc241;
}
@-moz-keyframes spin {
0% { -moz-transform: rotate(0deg); }
100% { -moz-transform: rotate(359deg); }
}
@-webkit-keyframes spin {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(359deg); }
}
@-o-keyframes spin {
0% { -o-transform: rotate(0deg); }
100% { -o-transform: rotate(359deg); }
}
@-ms-keyframes spin {
0% { -ms-transform: rotate(0deg); }
100% { -ms-transform: rotate(359deg); }
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(359deg); }
}
</style>
<script type="text/javascript" src="http://a.tbcdn.cn/s/kissy/1.3.0/kissy.js"></script>
</head>
<body>
<header>
<h1>图片上传</h1>
</header>
<section id="content">
<input type="file" id="jfile" />
<div class="imglist" id="jplus">
<p class="plus">+</p>
<p class="btn">增加图片</p>
</div>
<div class="describe">
<!--可以在这里增加多个需要回填的隐藏域-->
<div id="otherfield">
<textarea style="display:none" id="input-describe" class="jfield" name="describe" rows="4" placeholder="请输入图片描述"></textarea>
<input class="jfield" type="hidden" name="test1" value="test1" />
<input class="jfield" type="hidden" name="test2" value="test2" />
<input class="jfield" type="hidden" name="test3" value="test3" />
<button class="submitbtn" type="button" id="submit">上传图片</button>
</div>
</div>
</section>
<section id="loading">
正在上传图片,请稍后.. <span>/</span>
</section>
<section id="message">
图片上传成功,回到电脑上刷新页面即可看到结果。
</section>
<script>
KISSY.ready(function(S){
var $=S.all, file=$("#jfile"), imageType = /image.*/,
maximg = 10,
content = $("#content"),
plus = $("#jplus"),
tpl = '<div class="imglist"><p class="img">load..</p><a class="btn del">删除图片</a></div>',
//看看上传了几张图
imgcount = function(){
if(content.all(".imglist").length-1>=maximg){
plus.hide();
}else{
plus.show();
}
}
//是否回填描述
if(localStorage.getItem("uploaddescribe")){
$("#input-describe").val(localStorage.getItem("uploaddescribe"))
}
content.delegate("click",".del",function(ev){
$(ev.currentTarget).parent().remove();
});
document.getElementById("jfile").addEventListener("change", function(){
var files = this.files;
for (var i = 0, numFiles = files.length; i < numFiles; i++) {
var file = files[i];
if(file.type.match(imageType)) {
var item = $(tpl),
reader = new FileReader();
item.data("data-file",file)
item.insertBefore(plus);
//同步下照片数量
imgcount();
reader.onload= function(e){
var img = document.createElement("img"),
imgpanel = item.one(".img");
imgpanel.text("").append(img);
img.src= e.target.result;
}
reader.readAsDataURL(file);
}
//});
}
}, false);
file.on("change", function(ev){
});
plus.on("click", function(ev){
ev.halt();
file.fire("click");
});
$("#submit").on("click", function(ev){
var formData = new FormData();
//判断是否有图片
if(content.all(".imglist").length<=1){
alert("你还没有添加图片!");
return;
}
content.hide();
$("#loading").show();
//加入需要回填的filed
$("#otherfield .jfield").each(function(item){
formData.append(item.attr("name"), item.val());
});
//加入file
content.all(".imglist").each(function(item,index){
if(item.hasData("data-file")){
formData.append("img"+index,item.data("data-file"));
}
});
var xhr = new XMLHttpRequest();
//这里配置后端接收图片的地址
//上传后返回json格式
//{"status":false,"message":"图片太大了"}
//{"status":true,"message":"上传成功"}
xhr.open("POST", "tree.html");
xhr.onload = function(d) {
var data={};
try{
data= new Function('return ' + data + ';')();
}catch(e){
//throw 'Json parse error!';
}
if(data && data.status) {
//上传成功
$("#message").show();
}else{
localStorage.setItem("uploaddescribe",$("#input-describe").val());
if(data && data.message){
alert(data.message);
}else{
alert("异常错误,请在页面刷新后重新上传");
}
location.reload();
}
};
xhr.send(formData);
})
});
</script>
</body>
</html>