This tutorial explains how to upload a file with bottle and normal HTML form. But how do I upload a file to a server with a jquery AJAX call?
HTML
<form id="uploadForm">
file: <input id="fileData" type="file" name="upload" />
<input type="submit" value="Start upload" />
</form>
jQuery
$("#uploadForm").submit(function(event) {
event.preventDefault();
$.ajax(
{
Type: "POST",
contentType: false,
url: "../upload",
data: //What do i pass here?,
success: function (data) {
alert(data);
}
});
})
Python Bottle
@app.route('/upload', method='POST')
def do_upload():
upload = bottle.request.files.query('upload')
sys.stdout.write(bottle.request.files.get('upload').filename);
name, ext = os.path.splitext(upload.filename)
inputfile = open('/srv/http/params/%s'%upload.filename, 'wb')
inputfile.write(upload.file.read())
inputfile.close()
return 'OK'
I'm mainly stuck at what data I should pass over to the AJAX call and how to retrieve this data at Bottle's side thereafter.