質問するログイン新規登録

質問編集履歴

1

ドラッグ&ドロップの部分の追記

2015/08/24 05:53

投稿

shinoda
shinoda

スコア75

title CHANGED
File without changes
body CHANGED
@@ -47,4 +47,239 @@
47
47
  ドラッグ&ドロップでファイルをアップロードする機能は調べたところいろいろあるのはわかったのですが、どうしてもこの中に組み込むのができなくて困っています。
48
48
 
49
49
  ご教授いただければ幸いです。
50
- 以上、宜しくお願い致します。
50
+ 以上、宜しくお願い致します。
51
+
52
+
53
+ 【追記】
54
+ ドラッグ&ドロップする機能の部分を記載します。
55
+ 長くなりますがご了承ください。
56
+
57
+ 簡単に言ってしまうとこの2つを上手く融合したいのですが、うまくいかないということです。
58
+
59
+ --------------------------------------
60
+ <!DOCTYPE html>
61
+ <html>
62
+ <head>
63
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
64
+ <style>
65
+ #dragandrophandler
66
+ {
67
+ border:2px dotted #0B85A1;
68
+ width:500px;
69
+ height:300px;
70
+ color:#92AAB0;
71
+ text-align:center;
72
+ vertical-align:middle;
73
+ padding:10px 10px 10 10px;
74
+ margin: 0 auto;
75
+ font-size:200%;
76
+ }
77
+ .progressBar {
78
+ width: 200px;
79
+ height: 22px;
80
+ border: 1px solid #ddd;
81
+ border-radius: 5px;
82
+ overflow: hidden;
83
+ display:inline-block;
84
+ margin:0px 10px 5px 5px;
85
+ vertical-align:top;
86
+ }
87
+
88
+ .progressBar div {
89
+ height: 100%;
90
+ color: #fff;
91
+ text-align: right;
92
+ line-height: 22px; /* same as #progressBar height if we want text middle aligned */
93
+ width: 0;
94
+ background-color: #0ba1b5; border-radius: 3px;
95
+ }
96
+ .statusbar
97
+ {
98
+ border-top:1px solid #A9CCD1;
99
+ min-height:25px;
100
+ width:700px;
101
+ padding:10px 10px 0px 10px;
102
+ vertical-align:top;
103
+ }
104
+ .statusbar:nth-child(odd){
105
+ background:#EBEFF0;
106
+ }
107
+ .filename
108
+ {
109
+ display:inline-block;
110
+ vertical-align:top;
111
+ width:250px;
112
+ }
113
+ .filesize
114
+ {
115
+ display:inline-block;
116
+ vertical-align:top;
117
+ color:#30693D;
118
+ width:100px;
119
+ margin-left:10px;
120
+ margin-right:5px;
121
+ }
122
+ .abort{
123
+ background-color:#A8352F;
124
+ -moz-border-radius:4px;
125
+ -webkit-border-radius:4px;
126
+ border-radius:4px;display:inline-block;
127
+ color:#fff;
128
+ font-family:arial;font-size:13px;font-weight:normal;
129
+ padding:4px 15px;
130
+ cursor:pointer;
131
+ vertical-align:top
132
+ }
133
+ </style>
134
+ </head>
135
+
136
+ <body>
137
+ <div id="dragandrophandler">ここにドロップしてください。</div>
138
+ <br><br>
139
+ <div id="status1"></div>
140
+ <script>
141
+ function sendFileToServer(formData,status)
142
+ {
143
+ var uploadURL ="http://hayageek.com/examples/jquery/drag-drop-file-upload/upload.php"; //Upload URL
144
+ var extraData ={}; //Extra Data.
145
+ var jqXHR=$.ajax({
146
+ xhr: function() {
147
+ var xhrobj = $.ajaxSettings.xhr();
148
+ if (xhrobj.upload) {
149
+ xhrobj.upload.addEventListener('progress', function(event) {
150
+ var percent = 0;
151
+ var position = event.loaded || event.position;
152
+ var total = event.total;
153
+ if (event.lengthComputable) {
154
+ percent = Math.ceil(position / total * 100);
155
+ }
156
+ //Set progress
157
+ status.setProgress(percent);
158
+ }, false);
159
+ }
160
+ return xhrobj;
161
+ },
162
+ url: uploadURL,
163
+ type: "POST",
164
+ contentType:false,
165
+ processData: false,
166
+ cache: false,
167
+ data: formData,
168
+ success: function(data){
169
+ status.setProgress(100);
170
+
171
+ $("#status1").append("File upload Done<br>");
172
+ }
173
+ });
174
+
175
+ status.setAbort(jqXHR);
176
+ }
177
+
178
+ var rowCount=0;
179
+ function createStatusbar(obj)
180
+ {
181
+ rowCount++;
182
+ var row="odd";
183
+ if(rowCount %2 ==0) row ="even";
184
+ this.statusbar = $("<div class='statusbar "+row+"'></div>");
185
+ this.filename = $("<div class='filename'></div>").appendTo(this.statusbar);
186
+ this.size = $("<div class='filesize'></div>").appendTo(this.statusbar);
187
+ this.progressBar = $("<div class='progressBar'><div></div></div>").appendTo(this.statusbar);
188
+ this.abort = $("<div class='abort'>中断</div>").appendTo(this.statusbar);
189
+ obj.after(this.statusbar);
190
+
191
+ this.setFileNameSize = function(name,size)
192
+ {
193
+ var sizeStr="";
194
+ var sizeKB = size/1024;
195
+ if(parseInt(sizeKB) > 1024)
196
+ {
197
+ var sizeMB = sizeKB/1024;
198
+ sizeStr = sizeMB.toFixed(2)+" MB";
199
+ }
200
+ else
201
+ {
202
+ sizeStr = sizeKB.toFixed(2)+" KB";
203
+ }
204
+
205
+ this.filename.html(name);
206
+ this.size.html(sizeStr);
207
+ }
208
+ this.setProgress = function(progress)
209
+ {
210
+ var progressBarWidth =progress*this.progressBar.width()/ 100;
211
+ this.progressBar.find('div').animate({ width: progressBarWidth }, 10).html(progress + "% ");
212
+ if(parseInt(progress) >= 100)
213
+ {
214
+ this.abort.hide();
215
+ }
216
+ }
217
+ this.setAbort = function(jqxhr)
218
+ {
219
+ var sb = this.statusbar;
220
+ this.abort.click(function()
221
+ {
222
+ jqxhr.abort();
223
+ sb.hide();
224
+ });
225
+ }
226
+ }
227
+ function handleFileUpload(files,obj)
228
+ {
229
+ for (var i = 0; i < files.length; i++)
230
+ {
231
+ var fd = new FormData();
232
+ fd.append('file', files[i]);
233
+
234
+ var status = new createStatusbar(obj); //Using this we can set progress.
235
+ status.setFileNameSize(files[i].name,files[i].size);
236
+ sendFileToServer(fd,status);
237
+
238
+ }
239
+ }
240
+ $(document).ready(function()
241
+ {
242
+ var obj = $("#dragandrophandler");
243
+ obj.on('dragenter', function (e)
244
+ {
245
+ e.stopPropagation();
246
+ e.preventDefault();
247
+ $(this).css('border', '2px solid #0B85A1');
248
+ });
249
+ obj.on('dragover', function (e)
250
+ {
251
+ e.stopPropagation();
252
+ e.preventDefault();
253
+ });
254
+ obj.on('drop', function (e)
255
+ {
256
+
257
+ $(this).css('border', '2px dotted #0B85A1');
258
+ e.preventDefault();
259
+ var files = e.originalEvent.dataTransfer.files;
260
+
261
+ //We need to send dropped files to Server
262
+ handleFileUpload(files,obj);
263
+ });
264
+ $(document).on('dragenter', function (e)
265
+ {
266
+ e.stopPropagation();
267
+ e.preventDefault();
268
+ });
269
+ $(document).on('dragover', function (e)
270
+ {
271
+ e.stopPropagation();
272
+ e.preventDefault();
273
+ obj.css('border', '2px dotted #0B85A1');
274
+ });
275
+ $(document).on('drop', function (e)
276
+ {
277
+ e.stopPropagation();
278
+ e.preventDefault();
279
+ });
280
+
281
+ });
282
+ </script>
283
+ </body>
284
+ </html>
285
+ ------------------------------------------------