Created
October 23, 2014 09:09
-
-
Save cescoffier/5b9f66bd49205fb23d4f to your computer and use it in GitHub Desktop.
Ajax-based file upload with Wisdom Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
#%L | |
Wisdom-Framework | |
%% | |
Copyright (C) 2013 - 2014 Wisdom Framework | |
%% | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
#L% | |
--> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>My Wisdom-based file server</title> | |
<link rel="stylesheet" href="/libs/css/bootstrap.min.css"/> | |
<link rel="stylesheet" href="/libs/css/bootstrap-theme.min.css"/> | |
<script src="/libs/jquery.js"></script> | |
<script src="/libs/js/bootstrap.min.js"></script> | |
<script src="/libs/jquery-ui.js"></script> | |
<script src="//blueimp.github.io/JavaScript-Load-Image/js/load-image.all.min.js"></script> | |
<script src="/libs/js/jquery.fileupload.js"></script> | |
<script src="/libs/js/jquery.fileupload-process.js"></script> | |
<script src="/libs/js/jquery.fileupload-image.js"></script> | |
<script src="/libs/js/jquery.fileupload-audio.js"></script> | |
<script src="/libs/js/jquery.fileupload-video.js"></script> | |
<script src="/libs/js/jquery.fileupload-validate.js"></script> | |
</head> | |
<style> | |
.fileinput-button { | |
position: relative; | |
overflow: hidden; | |
} | |
.fileinput-button input { | |
position: absolute; | |
top: 0; | |
right: 0; | |
margin: 0; | |
opacity: 0; | |
-ms-filter: 'alpha(opacity=0)'; | |
font-size: 200px; | |
direction: ltr; | |
cursor: pointer; | |
} | |
</style> | |
<body> | |
<div class="container"> | |
<div class="starter-template"> | |
<h1>My Wisdom-based file server</h1> | |
<!-- if flash is set, print the flash message --> | |
<div class="flash bg-success" th:if="${message}"> | |
<p th:text="${message}"> | |
Flash message | |
</p> | |
</div> | |
<!-- The fileinput-button span is used to style the file input field as button --> | |
<span class="btn btn-success fileinput-button"> | |
<i class="glyphicon glyphicon-plus"></i> | |
<span>Add files...</span> | |
<!-- The file input field used as target for the file upload widget --> | |
<input id="fileupload" type="file" name="file" multiple="true"/> | |
</span> | |
<br/> | |
<br/> | |
<!-- The global progress bar --> | |
<div id="progress" class="progress"> | |
<div class="progress-bar progress-bar-success"></div> | |
</div> | |
<!-- The container for the uploaded files --> | |
<div id="files" class="files"></div> | |
<!-- list of files --> | |
<div> | |
<div> | |
<h3>Files:</h3> | |
</div> | |
<table class="table table-stripped"> | |
<tr> | |
<th>Name</th> | |
<th>Size</th> | |
<th>Download link</th> | |
</tr> | |
<tr th:each="file : ${files}"> | |
<td th:text="${file.getName()}">Sample Name</td> | |
<td th:text="${file.size}">Description</td> | |
<td><a href="#" th:attr="href=${#routes.route('download', 'name', file.name)}">link</a></td> | |
</tr> | |
</table> | |
</div> | |
</div> | |
</div> | |
</body> | |
<script th:inline="javascript"> | |
/*<![CDATA[*/ | |
/*jslint unparam: true, regexp: true */ | |
/*global window, $ */ | |
$(function () { | |
'use strict'; | |
// Change this to the location of your server-side upload handler: | |
var url = "/file2", | |
uploadButton = $('<button/>') | |
.addClass('btn btn-primary') | |
.prop('disabled', true) | |
.text('Processing...') | |
.on('click', function () { | |
var $this = $(this), | |
data = $this.data(); | |
$this | |
.off('click') | |
.text('Abort') | |
.on('click', function () { | |
$this.remove(); | |
data.abort(); | |
}); | |
data.submit().always(function () { | |
$this.remove(); | |
}); | |
}); | |
$('#fileupload').fileupload({ | |
url: url, | |
dataType: 'json', | |
autoUpload: false, | |
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i, | |
maxFileSize: 5000000, // 5 MB | |
// Enable image resizing, except for Android and Opera, | |
// which actually support image resizing, but fail to | |
// send Blob objects via XHR requests: | |
disableImageResize: /Android(?!.*Chrome)|Opera/ | |
.test(window.navigator.userAgent), | |
previewMaxWidth: 100, | |
previewMaxHeight: 100, | |
previewCrop: true | |
}).on('fileuploadadd', function (e, data) { | |
data.context = $('<div/>').appendTo('#files'); | |
$.each(data.files, function (index, file) { | |
var node = $('<p/>') | |
.append($('<span/>').text(file.name)); | |
if (!index) { | |
node | |
.append('<br/>') | |
.append(uploadButton.clone(true).data(data)); | |
} | |
node.appendTo(data.context); | |
}); | |
}).on('fileuploadprocessalways', function (e, data) { | |
var index = data.index, | |
file = data.files[index], | |
node = $(data.context.children()[index]); | |
if (file.preview) { | |
node | |
.prepend('<br/>') | |
.prepend(file.preview); | |
} | |
if (file.error) { | |
node | |
.append('<br/>') | |
.append($('<span class="text-danger"/>').text(file.error)); | |
} | |
if (index + 1 === data.files.length) { | |
data.context.find('button') | |
.text('Upload') | |
.prop('disabled', !!data.files.error); | |
} | |
}).on('fileuploadprogressall', function (e, data) { | |
var progress = parseInt(data.loaded / data.total * 100, 10); | |
$('#progress .progress-bar').css( | |
'width', | |
progress + '%' | |
); | |
}).on('fileuploadfail', function (e, data) { | |
$.each(data.files, function (index) { | |
console.log(e); | |
var error = $('<span class="text-danger"/>').text('File upload failed.'); | |
$(data.context.children()[index]) | |
.append('<br/>') | |
.append(error); | |
}); | |
}).prop('disabled', !$.support.fileInput) | |
.parent().addClass($.support.fileInput ? undefined : 'disabled'); | |
}); | |
/*]]>*/ | |
</script> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Requires | |
Json json; | |
@Route(method = HttpMethod.POST, uri = "/file2") | |
public Result upload2(final @FormParameter("file") FileItem file) throws | |
IOException { | |
if (file == null) { | |
logger().error("No uploaded file !"); | |
flash("error", "true"); | |
flash("message", "No uploaded file"); | |
return badRequest(index()); | |
} | |
// This should be asynchronous. | |
return async(new Callable<Result>() { | |
@Override | |
public Result call() throws Exception { | |
// logger().info("uploaded file : {}", file.name()); | |
File out = new File(root, file.name()); | |
FileUtils.copyInputStreamToFile(file.stream(), out); | |
flash("success", "true"); | |
flash("message", "File " + file.name() + " uploaded (" + out.length() + " bytes)"); | |
return ok(json.newObject().put("success", true).put("file", out.getAbsolutePath())); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment