easyui.hp.multipartImageUpload.html
5.93 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
<!DOCTYPE html>
<div class="easyui-layout" fit="true" id="multipartImageUploadDialog">
<div data-options="region:'north'" style="height:42px;" id="multipartImageUploadRegionNorth">
<div style="float:left;height:100%;background-color: red" id="multipartImageUploadMessageDiv">
<span style="height: 100%">asdfasdfasdf</span>
</div>
<div style="float:right;height:100%">
<div class="multipart-image-upload">
<input type="file" id="multipartImageUploadFile" style="display: none" />
<a id="multipartImageUploadAddImage" class="btn-success"></a>
</div>
<a id="multipartImageUploadUploadImage" class="btn-info" style="margin-left: 10px;margin-right: 10px"></a>
</div>
</div>
<div data-options="region:'center'" style="padding:5px" id="multipartImageUploadRegionCenter">
<ul class="filelist" id="multipartImageUploadImageList"></ul>
</div>
</div>
<script>
$(function() {
var opt = $.fn.multipartImageUpload.tempParam;
var multipartImageUploadFile = $("#multipartImageUploadDialog #multipartImageUploadFile");
multipartImageUploadFile.attr("accept", opt.accept);
if (opt.multiple === true) {
multipartImageUploadFile.attr("multiple", "multiple")
}
function createImage(item, isUploaded) {
var li = $("<li>");
var div = $("<div>").addClass("img-div").appendTo(li);
//var label = $("<div>").appendTo(li).append($("<p>").html(item.name));
if (isUploaded === true) {
div.append($("<input type='hidden' role='img'>").val(item.url));
}
div.append($("<input type='hidden' role='name'>").val(item.name));
var img = $("<img>").attr({
src : item.url
}).appendTo(div);
//div.append($("<span>").addClass("file-close"));
var tools = $("<div>").appendTo(div).addClass("tool-panel").hide();
var cancel = $("<span>").addClass("cancel").appendTo(tools);
var handleDiv = $("<div>").addClass("handel-panel").appendTo(div);
cancel.click(function() {
li.remove();
});
li.hover(function() {
tools.slideDown();
}, function() {
tools.slideUp();
});
li.draggable({
proxy : "clone",
revert : true,
handle : ".handel-panel",
cursor : "auto",
onStartDrag : function() {
$(this).draggable("proxy").addClass("multipart-image-upload-draggable-proxy");
$(this).draggable("proxy").get(0).offsetTop = $(this).draggable("proxy").get(0).offsetTop - li.parent().scrollTop();
},
onStopDrag : function() {
//$(this).draggable("options").cursor = "auto";
},
onDrag : function(e) {
var d = e.data;
if (d.left < 0) {
d.left = 0;
}
if (d.top < 0) {
d.top = 0;
}
d.top = d.top - $(d.parent).scrollTop();
if (d.left + $(d.target).outerWidth() >= $(d.parent).outerWidth()) {
d.left = $(d.parent).outerWidth() - $(d.target).outerWidth();
}
}
});
li.droppable({
accept : "#multipartImageUploadImageList li",
onDragEnter : function(e, source) {
$(source).draggable("proxy").css("border", "2px solid red");
$(this).css("border", "2px solid green");
},
onDragLeave : function(e, source) {
$(source).draggable("proxy").css("border", "2px solid white");
$(this).css("border", "2px solid white");
},
onDrop : function(e, source) {
$(source).draggable("proxy").css("border", "2px solid white");
$(this).css("border", "2px solid white");
var tmpSource = $(source).clone();
tmpSource.insertBefore($(source));
$(source).insertBefore($(this));
$(this).detach().insertBefore(tmpSource);
tmpSource.remove();
}
});
return li;
}
multipartImageUploadFile.change(function() {
$.myPlugin.convertImgToBase64(this, function(arr) {
if (!arr || arr.length == 0) {
return;
}
$(arr).each(function(index, item) {
$("#multipartImageUploadDialog #multipartImageUploadImageList").append(createImage(item));
});
});
});
$("#multipartImageUploadDialog #multipartImageUploadAddImage").linkbutton({
text : "选择图片",
iconCls : "icon-add",
onClick : function() {
$(this).prev().click();
}
});
$("#multipartImageUploadDialog #multipartImageUploadUploadImage").linkbutton({
text : "开始上传",
iconCls : "icon-redo",
onClick : function() {
var imagesList = $("#multipartImageUploadImageList div.img-div");
if (!imagesList || imagesList.length == 0) {
$.messager.alert("提示", "没有需要上传的文件", "error");
return;
}
var arr = [];
$(imagesList).each(function(index, item) {
if ($(item).has("input[type='hidden'][role='img']").length > 0) {
return true;
}
arr.push(item);
});
if (arr.length == 0) {
$.messager.alert("提示", "没有需要上传的文件", "error");
return;
}
var result = [], error = "";
$.messager.progress({
title : "正在执行",
msg : "正在执行,请稍后..."
});
$(arr).each(function(index, item) {
$.post(opt.uploadUrl, {
base64Str : $(item).find("img").attr("src"),
originalFilename : $(item).find("input[type='hidden'][role='name']").val()
}, function(json) {
if (!json || json.code != 200) {
error = json.message;
$(item).parent().css("border", "2px solid red");
return;
}
$(item).parent().css("border", "2px solid white");
$(item).find("img").attr("src", json.data.url);
$(item).append($("<input type='hidden' role='img'>").val(json.data.url));
result.push($.extend({}, item, {
url : json.data.url
}));
});
});
var interval = window.setInterval(function() {
if (error !== "") {
$.messager.progress("close");
$.messager.alert("提示", error, "error");
clearInterval(interval);
return;
}
if (result.length == arr.length) {
clearInterval(interval);
$.messager.progress("close");
//opt.uploadSuccess.call(this, result);
}
}, 100);
}
});
//alert(JSON.stringify($.fn.multipartImageUpload.tempParam));
});
</script>