2015-12-16 38 views
1

如何發送文件作爲對使用瓶框架的Python中的HTTP請求的答案?用Python框架通過http發送文件與瓶框架

上傳此代碼的工作對我來說:

@route('/upload', method='POST') 
def do_upload(): 
    category = request.forms.get('category') 
    upload  = request.files.get('upload') 
    name, ext = os.path.splitext(upload.raw_filename) 
    if ext not in ('.png','.jpg','.jpeg'): 
     return 'File extension not allowed.' 

    save_path = category 
    upload.raw_filename = "hoho" + ext 
    upload.save(save_path) # appends upload.filename automatically 
    return 'OK' 

回答

0

只要打電話Bottle's static_file function並返回結果:

@route('/static/<filename:path>') 
def send_static(filename): 
    return static_file(filename, root='/path/to/static/files') 

這個例子,當網址/static/myfile.txt調用,將返回文件的內容/path/to/static/files/myfile.txt

其他SO問題(如this one)包含其他示例。