uploadImg-v1.js
10.8 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
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
var imgFile = []; //文件流
var imgSrc = []; //图片路径
var imgName = []; //图片名字
var imgUrl=[];//图片地址-访问地址
var imgFile2 = []; //文件流
var imgSrc2 = []; //图片路径
var imgName2 = []; //图片名字
var imgUrl2=[];//图片地址-访问地址
var imgFile3 = []; //文件流
var imgSrc3 = []; //图片路径
var imgName3 = []; //图片名字
var imgUrl3=[];//图片地址-访问地址
$(function() {
// 鼠标经过显示删除按钮
$('#img1').on('mouseover', '.content-img-list-item', function() {
$(this).children('div').removeClass('hide');
});
$('#img2').on('mouseover', '.content-img-list-item', function() {
$(this).children('div').removeClass('hide');
});
$('#img3').on('mouseover', '.content-img-list-item', function() {
$(this).children('div').removeClass('hide');
});
// 鼠标离开隐藏删除按钮
$('#img1').on('mouseleave', '.content-img-list-item', function() {
$(this).children('div').addClass('hide');
});
$('#img2').on('mouseleave', '.content-img-list-item', function() {
$(this).children('div').addClass('hide');
});
$('#img3').on('mouseleave', '.content-img-list-item', function() {
$(this).children('div').addClass('hide');
});
// 单个图片删除
$("#img1").on("click", '.content-img-list-item a .gcllajitong', function() {
var index = $(this).parent().parent().parent().index();
imgSrc.splice(index, 1);
imgFile.splice(index, 1);
imgName.splice(index, 1);
var boxId = "#img1";
addNewContent(boxId);
if (imgSrc.length < 4) { //显示上传按钮
$('.content-img .file').show();
}
});
$("#img2").on("click", '.content-img-list-item a .gcllajitong', function() {
var index = $(this).parent().parent().parent().index();
imgSrc2.splice(index, 1);
imgFile2.splice(index, 1);
imgName2.splice(index, 1);
var boxId = "#img2";
addNewContent2(boxId);
if (imgSrc2.length < 4) { //显示上传按钮
$('.content-img .file').show();
}
});
$("#img3").on("click", '.content-img-list-item a .gcllajitong', function() {
var index = $(this).parent().parent().parent().index();
imgSrc3.splice(index, 1);
imgFile3.splice(index, 1);
imgName3.splice(index, 1);
var boxId = "#img3";
addNewContent3(boxId);
if (imgSrc3.length < 4) { //显示上传按钮
$('.content-img .file').show();
}
});
$("#img1").on("click", '.content-img-list-item a .gclfangda', function() {
var index = $(this).parent().parent().parent().index();
$(".modal-content").html("");
var bigimg = $(".modal-content").html();
$(".modal-content").html(bigimg + '<div class="show"><img src="' + imgSrc[index] + '" alt=""><div>');
// $(".modal-content").append(
// '<div class="show"><img src="' + imgSrc[a] + '" alt=""><div>'
// );
});
$("#img2").on("click", '.content-img-list-item a .gclfangda', function() {
var index = $(this).parent().parent().parent().index();
$(".modal-content").html("");
var bigimg = $(".modal-content").html();
$(".modal-content").html(bigimg + '<div class="show"><img src="' + imgSrc2[index] + '" alt=""><div>');
// $(".modal-content").append(
// '<div class="show"><img src="' + imgSrc[a] + '" alt=""><div>'
// );
});
$("#img3").on("click", '.content-img-list-item a .gclfangda', function() {
var index = $(this).parent().parent().parent().index();
$(".modal-content").html("");
var bigimg = $(".modal-content").html();
$(".modal-content").html(bigimg + '<div class="show"><img src="' + imgSrc3[index] + '" alt=""><div>');
// $(".modal-content").append(
// '<div class="show"><img src="' + imgSrc[a] + '" alt=""><div>'
// );
});
});
//图片上传
$('#upload').on('change', function(e) {
var imgSize = this.files[0].size;
if (imgSize > 1024 * 1000 * 1) { //1M
return alert("上传图片不能超过1000KB");
};
if (this.files[0].type != 'image/png' && this.files[0].type != 'image/jpeg' && this.files[0].type != 'image/gif') {
return alert("图片上传格式不正确");
}
var imgBox = '#img1';
var fileList = this.files;
for (var i = 0; i < fileList.length; i++) {
var imgSrcI = getObjectURL(fileList[i]);
imgName.push(fileList[i].name);
imgSrc.push(imgSrcI);
imgFile.push(fileList[i]);
}
addNewContent(imgBox);
uploadPic();
this.value = null; //上传相同图片
});
$('#upload2').on('change', function(e) {
var imgSize = this.files[0].size;
if (imgSize > 1024 * 1000 * 1) { //1M
return alert("上传图片不能超过1000KB");
};
if (this.files[0].type != 'image/png' && this.files[0].type != 'image/jpeg' && this.files[0].type != 'image/gif') {
return alert("图片上传格式不正确");
}
var imgBox = '#img2';
var fileList = this.files;
for (var i = 0; i < fileList.length; i++) {
var imgSrcI = getObjectURL(fileList[i]);
imgName2.push(fileList[i].name);
imgSrc2.push(imgSrcI);
imgFile2.push(fileList[i]);
}
addNewContent2(imgBox);
uploadPic2();
this.value = null; //上传相同图片
});
$('#upload3').on('change', function(e) {
var imgSize = this.files[0].size;
if (imgSize > 1024 * 1000 * 1) { //1M
return alert("上传图片不能超过1000KB");
};
if (this.files[0].type != 'image/png' && this.files[0].type != 'image/jpeg' && this.files[0].type != 'image/gif') {
return alert("图片上传格式不正确");
}
var imgBox = '#img3';
var fileList = this.files;
for (var i = 0; i < fileList.length; i++) {
var imgSrcI = getObjectURL(fileList[i]);
imgName3.push(fileList[i].name);
imgSrc3.push(imgSrcI);
imgFile3.push(fileList[i]);
}
addNewContent3(imgBox);
uploadPic3();
this.value = null; //上传相同图片
});
function uploadPic(){
var formFile = new FormData();
formFile.append('file', imgFile[imgFile.length-1]);
$.ajax({
type: "post",
dataType: "json",
contentType:false,
processData:false,
dataType:"json",
url:prefix+"/answer/upload",
data: formFile,
success:function(r) {
if (r.code == 200) {
var picUrl=r.data.url;
var pics=$("input[name='pic']");
console.log(pics.length)
console.log( pics)
imgUrl.push(picUrl);
pics[pics.length-1].value=picUrl;
layer.msg('上传成功')
}
}
})
}
function uploadPic2(){
var formFile = new FormData();
formFile.append('file', imgFile2[imgFile2.length-1]);
$.ajax({
type: "post",
dataType: "json",
contentType:false,
processData:false,
dataType:"json",
url:prefix+"/answer/upload",
data: formFile,
success:function(r) {
if (r.code == 200) {
var picUrl=r.data.url;
var pics=$("input[name='pic2']");
console.log(picUrl)
console.log( pics)
imgUrl2.push(picUrl);
pics[pics.length-1].value=picUrl;
layer.msg('上传成功')
}
}
})
}
function uploadPic3(){
var formFile = new FormData();
formFile.append('file', imgFile3[imgFile3.length-1]);
$.ajax({
type: "post",
dataType: "json",
contentType:false,
processData:false,
dataType:"json",
url:prefix+"/answer/upload",
data: formFile,
success:function(r) {
if (r.code == 200) {
var picUrl=r.data.url;
var pics=$("input[name='pic3']");
console.log(picUrl)
console.log( pics)
imgUrl3.push(picUrl);
pics[pics.length-1].value=picUrl;
layer.msg('上传成功')
}
}
})
}
//删除
function removeImg(obj, index) {
imgSrc.splice(index, 1);
imgFile.splice(index, 1);
imgName.splice(index, 1);
picUrl.splice(index,1);
var boxId = ".content-img-list";
addNewContent(boxId);
}
//图片展示
function addNewContent(obj) {
// console.log(imgSrc)
$(obj).html("");
for (var a = 0; a < imgSrc.length; a++) {
var oldBox = $(obj).html();
var pichtml=pichtml='<input type="hidden" name="pic" value="'+imgUrl[a]+'">';
$(obj).html(oldBox + '<li class="content-img-list-item"><img src="' + imgSrc[a] + '" alt="">' +
pichtml+
'<div class="hide"><a index="' + a + '" class="delete-btn"><i class="gcl gcllajitong"></i></a>' +
'<a index="' + a + '" class="big-btn" type="button" data-toggle="modal" data-target=".bs-example-modal-lg"><i class="gcl gclfangda"></i></a></div></li>');
}
}
function addNewContent2(obj) {
console.log(imgSrc)
console.log(imgUrl2)
$(obj).html("");
for (var a = 0; a < imgSrc2.length; a++) {
var oldBox = $(obj).html();
var pichtml=pichtml='<input type="hidden" name="pic2" value="'+imgUrl2[a]+'">';
$(obj).html(oldBox + '<li class="content-img-list-item"><img src="' + imgSrc2[a] + '" alt="">' +
pichtml+
'<div class="hide"><a index="' + a + '" class="delete-btn"><i class="gcl gcllajitong"></i></a>' +
'<a index="' + a + '" class="big-btn" type="button" data-toggle="modal" data-target=".bs-example-modal-lg"><i class="gcl gclfangda"></i></a></div></li>');
}
}
function addNewContent3(obj) {
console.log(imgSrc)
console.log(imgUrl3)
$(obj).html("");
for (var a = 0; a < imgSrc3.length; a++) {
var oldBox = $(obj).html();
var pichtml=pichtml='<input type="hidden" name="pic3" value="'+imgUrl3[a]+'">';
$(obj).html(oldBox + '<li class="content-img-list-item"><img src="' + imgSrc3[a] + '" alt="">' +
pichtml+
'<div class="hide"><a index="' + a + '" class="delete-btn"><i class="gcl gcllajitong"></i></a>' +
'<a index="' + a + '" class="big-btn" type="button" data-toggle="modal" data-target=".bs-example-modal-lg"><i class="gcl gclfangda"></i></a></div></li>');
}
}
//建立可存取到file的url
function getObjectURL(file) {
var url = null;
if (window.createObjectURL != undefined) { // basic
url = window.createObjectURL(file);
} else if (window.URL != undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file);
} else if (window.webkitURL != undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file);
}
return url;
}