I am trying to upload a file to a cherrypy server, but without changing the displayed page. A simple alert will be enough for my needs to tell the user that the upload is done.
I am trying this with a simple file input type and a button:
<div>
<label>Upload Sound:</label>
<input id="pathSound" style="width: 80%" type="file" accept=".mp3"/><br>
<button id="uploadSound">Upload</button>
</div>
and a script for the button click:
$("#uploadSound").click(function(e) {
if (document.getElementById("pathSound").value=='') {
alert("No file selected!");
e.preventDefault();
return;
}
var cardID = $('#tagID').html();
var file = document.getElementById("pathSound").files[0];
alert( "Tag: " + cardID + " File: " + file);
$.post("/uploadSound", {"cardID": cardID, "myFile": file})
.done(function(res) {
alert("File Saved!");
});
});
on the server side i have this function so far, but it is never called:
import os, os.path
import cherrypy
from cherrypy.process import plugins
class MagicBoxInterface(object):
@cherrypy.expose
def index(self):
return file('index.html')
@cherrypy.expose
def uploadSound(self, cardID='', myFile=None):
print 'uploadSound : ', cardID
print 'uploadSound : ', myFile
return ''
if __name__ == '__main__':
conf = {
'/': {
'tools.sessions.on': True,
'tools.staticdir.root': os.path.abspath(os.getcwd())
},
'/static': {
'tools.staticdir.on': True,
'tools.staticdir.dir': './public'
}
}
interface = MagicBoxInterface()
cherrypy.quickstart(interface, '/', conf)
All the examples i find use normal post and show a result page after the upload, but this is not what i want to do! The displayed page must not change.
On a side note, i don't need to consider large files, because it will only handle files below 1-2 MB.
I have found some examples using frames, but i am not that familiar with html/javascript to fully understand how i can apply those to my needs.
And help on a simple solution is greatly appreciated.