Uploading a file in ajax to CherryPy

前端 未结 1 1927
失恋的感觉
失恋的感觉 2021-01-15 19:05

I am trying to upload many files at once to my CherryPy server.

I am following this tutorial that shows PHP code on the server side.

The JavaScript part is

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-15 19:38

    At its minimum it looks like the following. Tested in Firefox and Chromium. If you need to support legacy browsers I'd look at some JavaScript library for polyfills and fallback.

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    
    import os
    import shutil
    
    import cherrypy
    
    
    config = {
      'global' : {
        'server.socket_host' : '127.0.0.1',
        'server.socket_port' : 8080,
        'server.thread_pool' : 8,
      }
    }
    
    
    class App:
    
      @cherrypy.expose
      def index(self):
        return '''
          
          
            CherryPy Async Upload
          
          
            
    ''' @cherrypy.expose def upload(self): '''Handle non-multipart upload''' filename = os.path.basename(cherrypy.request.headers['x-filename']) destination = os.path.join('/home/user', filename) with open(destination, 'wb') as f: shutil.copyfileobj(cherrypy.request.body, f) if __name__ == '__main__': cherrypy.quickstart(App(), '/', config)

    0 讨论(0)
提交回复
热议问题