Skip to content

Commit 98be99f

Browse files
committed
Added support for multiple file upload
Also fixed the upload form so the button changed to "Uploading..." during an upload and clears the form on success
1 parent 4aac056 commit 98be99f

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

app.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import collections, logging, os, requests, signal, time
1+
import collections, json, logging, os, requests, signal, time
22
from tornado import httpserver, ioloop, web
33

44

@@ -27,14 +27,17 @@ def delete(self):
2727

2828
class UploadHandler(web.RequestHandler):
2929
def post(self):
30-
file_des = self.request.files['file'][0]
31-
file_name = temp_img_prefix + file_des['filename']
32-
rel_path = static_img_path + file_name
33-
output_file = open(rel_path, 'wb')
34-
output_file.write(file_des['body'])
35-
output_file.close()
36-
caption = run_ml(rel_path)
37-
self.finish({"file_name": rel_path, "caption": caption})
30+
finish_ret = []
31+
new_files = self.request.files['file']
32+
for file_des in new_files:
33+
file_name = temp_img_prefix + file_des['filename']
34+
rel_path = static_img_path + file_name
35+
output_file = open(rel_path, 'wb')
36+
output_file.write(file_des['body'])
37+
output_file.close()
38+
caption = run_ml(rel_path)
39+
finish_ret.append({"file_name": rel_path, "caption": caption})
40+
self.finish(json.dumps(finish_ret))
3841

3942

4043
# Runs ML on given image

static/webapp.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
// Adds a new image to the image picker from the given return json of an upload
44
function add_thumbnails(data) {
5-
if (get_keys().includes(data["file_name"]) == false) {
6-
$('#thumbnails select').prepend($("<option></option>")
7-
.attr("data-img-src", data["file_name"])
8-
.attr("data-img-label", data["caption"])
9-
.attr("data-img-alt", data["caption"])
10-
.text(data["caption"]));
5+
key_list = get_keys()
6+
for (var i = 0; i < data.length; i++) {
7+
file_data = data[i];
8+
if (key_list.includes(file_data["file_name"]) == false) {
9+
$('#thumbnails select').prepend($("<option></option>")
10+
.attr("data-img-src", file_data["file_name"])
11+
.attr("data-img-label", file_data["caption"])
12+
.attr("data-img-alt", file_data["caption"])
13+
.text(file_data["caption"]));
14+
}
1115
}
1216
}
1317

@@ -183,6 +187,8 @@ $(function() {
183187
var form = event.target;
184188
var data = new FormData(form);
185189

190+
$("#file-submit").text("Uploading...")
191+
186192
// Perform file upload
187193
$.ajax({
188194
url: "/upload",
@@ -195,6 +201,8 @@ $(function() {
195201
success: function(data) {
196202
add_thumbnails(data);
197203
set_img_picker();
204+
$("#file-submit").text("Submit");
205+
$("#file-input").val("");
198206
}
199207
})
200208
})

templates/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ <h1>MAX Image Caption Generator</h1>
4848
<div class="panel-body">
4949
<form id="img-upload" enctype="multipart/form-data">
5050
<div class="form-group">
51-
<input id="file-input" type="file" name="file"/>
51+
<input id="file-input" type="file" name="file" multiple />
5252
</div>
53-
<button type="submit" class="btn btn-primary">Submit</button>
53+
<button id="file-submit" type="submit" class="btn btn-primary">Submit</button>
5454
</form>
5555
</div>
5656

0 commit comments

Comments
 (0)