【问题标题】:cherrypy+jquery ajax file uploadingcherrypy+jquery ajax 文件上传
【发布时间】:2012-10-06 08:07:24
【问题描述】:

现在我使用此代码在没有 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'

但我找不到任何使用 Cherrypy 和 Jquery 上传 ajax 文件的示例。 请帮忙!

【问题讨论】:

    标签: jquery cherrypy


    【解决方案1】:

    最后,我通过 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());
    });
    

    但我仍然无法获得上传进度条。 有什么建议吗?

    【讨论】:

      猜你喜欢
      • 2015-01-08
      • 2016-12-23
      • 1970-01-01
      • 2021-12-15
      相关资源
      最近更新 更多