0

m* cherrypy:python *y 项目在 iphone 应用程序上...它需要管理我需要上传图像并将其路径存储在数据库中的每个用户的个人资料。

我正在处理服务器端编码cherrypy framewok ..我需要通过http post获取图像并将其路径名存储在mysql数据库中...

你能给我代码和描述如何通过http post获取图像,如何表达图像,以及存储在数据库中……我是cherrypy框架的新手,我第一次使用这个……请帮帮我

我试过cherrypy doc如何上传文件:: http://docs.cherrypy.org/stable/progguide/files/uploading.html 但我做不到

4

1 回答 1

1

要使代码适用于您提供的链接,您需要在下面的代码中添加我的评论后的行...

def upload(self, myFile):
    MySQLconnection = MySQLdb.connect(host=cherrypy.request.app.config['Database']['host'], 
                                 passwd=cherrypy.request.app.config['Database']['passwd'],
                                 db=cherrypy.request.app.config['Database']['db'],
                                 user=cherrypy.request.app.config['Database']['user'],
                                 port=cherrypy.request.app.config['Database']['port'], 
                                 cursorclass=DictCursor)
    MySQLcursor = MySQLconnection.cursor()
    size = 0

    # add this line
    all_data = bytearray()

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

        saved_file=open('upload_path', 'wb') 
        saved_file.write(all_data) 
        saved_file.close()

    MySQLcursor.execute("insert into ImagePathDatabase (path) values ('" + MySQLdb.escape_string(myFile.filename) + "')")
    MySQLcursor.execute("commit;")

如果您需要更多帮助,请告诉我。

安德鲁

于 2013-08-19T14:58:57.930 回答