how to make web.py as a file uplaod server?

41 views
Skip to first unread message

Johnny Jiang

unread,
May 10, 2013, 2:37:14 AM5/10/13
to we...@googlegroups.com
hi all,i'm a newbie to the web.py and now using it as a RESTful server,it works very well.

But now i  facing a problem,that i need to upload files to the server.

What i'm want to do is post a URL like:'http:/server/fileupload?file_path=c:/something/else',then upload file to the server and return where the file has be stored.

Is this possible?How can i do it ?I found it everywhere but got no solution.I need help~

Thanks.

Johnny Jiang

unread,
May 11, 2013, 2:11:46 AM5/11/13
to we...@googlegroups.com
i'm already solved the problem.
In the server side,using these codes:
“”“JUST POST”“”
def POST(self):
        files = web.input(file_path={})
        if 'file_path' in files:
            """setting server side dir"""
            homedir = os.getcwd()
            filedir = '%s/static/upload/img' %homedir
            
            """rename filename"""
            filepath=files.file_path.replace('\\','/')
            ext=filepath.split('.', 1)[1]

            now=datetime.datetime.now()
            t ="%d%d%d%d%d%d" %(now.year,now.month,now.day,now.hour,now.minute,now.second)
            
            file_name = t+'.'+ext
            """read the imge file as binary format"""
            
            try:
                with open(files.file_path, 'rb') as f_in:
                    data = f_in.read()
                f_in.close()

                with open(filedir+'/'+file_name, 'wb') as f_out:
                    f_out.write(data)
                f_out.close()
            except:
                return 'faild'
            
            return '/static/upload/img/'+file_name

在 2013年5月10日星期五UTC+8下午2时37分14秒,Johnny Jiang写道:
Reply all
Reply to author
Forward
0 new messages