In my default contoller I have two functions that handle files:
- upload - takes the file upload and puts the file content into a variable in the session (or wherever you want it to be)
- docs - shows a list of uploaded files with information about the files
The I created a view that can be included with LOAD e.g. in default/index.html that uploads files (currently not possible in web2py)
For that I used the plugin at http://valums.com/ajax-upload/
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript" src="{{=URL(request.application,'static','ajaxupload.js')}}"></script>
<script type= "text/javascript">/*<![CDATA[*/
$(document).ready(function(){
var button = $('#upload_button'), interval;
new AjaxUpload(button,{
//action: 'upload-test.php', // I disabled uploads in this example for security reasons
action: "{{=URL(request.application,'default','upload')}}",
name: 'sbmlfile',
onSubmit : function(file, ext){
// change button text, when user selects file
button.text('Uploading');
// If you want to allow uploading only 1 file at time,
// you can disable upload button
this.disable();
// Uploding -> Uploading. -> Uploading...
interval = window.setInterval(function(){
var text = button.text();
if (text.length < 13){ button.text(text + '.');
} else { button.text('Uploading');
}
}, 200);
},
onComplete: function(file, response){
button.text('Upload SBML File');
window.clearInterval(interval);
// enable upload button
this.enable();
// add file to the list
web2py_component("{{=URL(request.application,'default','docs')}}","view_docs");
}
});
});/*]]>*/</script>
<div id="upload_button" class="button">Upload File</div>
{{=LOAD('default','docs',ajax = True, target = 'view_docs')}}
the interesting lines are the last line of the JavaScript code
web2py_component("{{=URL(request.application,'default','docs')}}","view_docs");
this line will refresh the list of uploaded documents that is displayed in the last line of the code with
{{=LOAD('default','docs',ajax = True, target = 'view_docs')}}
the rest is basically just copy and paste from the plugin code (except for the upload location)
mr.freeze
mr.freeze
jumi
paulgerrard