Search code examples
jquerycherrypy

cherrypy+jquery ajax file uploading


Now i use this code for uploading without ajax

@cherrypy.expose
def upload(self, file):
    uload_path = pm.get_package_download_dir()
    file_name = 'some.file'

    if not isdir(uload_path):
        try:
            makedirs(uload_path)
        except ValueError:
            raise cherrypy.HTTPError(400, 'SOME_ERROR')        

    uload_path = uload_path + os.path.sep + file_name                

    size = 0
    all_data = ''
    while True:
        data = file.file.read(8192)
        all_data += data
        if not data:
            break
        size += len(data)

    try:
        saved_file=open(uload_path, 'wb') 
        saved_file.write(all_data) 
        saved_file.close()
    except ValueError:
        raise cherrypy.HTTPError(400, 'SOME ERROR')

    print 'OK'

But I can't find any examples of ajax file uploading with Cherrypy and Jquery. Please help!


Solution

  • Finally, I have solved this problem via iframe

    <iframe id="iframe" name="iframe" style="display:none;"></iframe>
    <form id="upload" action="./upload" enctype="multipart/form-data" method="post" target="iframe">
      <input type="file" id="file" name="file">
      <input type="submit">
    </form>      
    
    $('#iframe').load(function(){
        console.log($(this).contents().find('#uploaded').val());
    });
    

    But I still can't get upload progress bar. Any suggestions?