2017-08-11 82 views
1

为了好玩,我正在制作上传/下载服务,但我一直在努力为静态目录之外的文件提供服务,因为任何人都可以访问www.mysite.com /静态并查看内容。从静态目录以外的瓶子提供瓶子文件

这是我到目前为止。请原谅我的网络存储:)

if not os.path.exists('\\ATTILA\\Development\\GIT\\MyCloud\\static\\'+ session['username']): 
     os.makedirs('\\\\ATTILA\\Development\\GIT\\MyCloud\\static\\'+ session['username']) 

    #Download links for all files 
    pathing = [] 
    my_path = '\\\\ATTILA\\Development\\GIT\\MyCloud\\static\\'+ session['username'] + '\\' 
    os.chdir('\\\\ATTILA\\Development\\GIT\\MyCloud\\static\\'+ session['username']) 
    for myfile in glob.glob('*'): 
     downs = my_path + myfile 
     pathing.append(tuple([downs, myfile])) 

在我的模板丑陋的路径,我有一个简单的for循环

{% for myfile, name in pathing %} 
    <a href='{{ myfile }}' download>{{ name }}</a><br> 
{% endfor %} 

所以我的看法是这样的:

enter image description here

由于它代表我的文件是可下载的,但是如果我将下载的文件路径更改为“静态”之外的文件夹,那么不用下载链接,我会得到404错误,即po诠释到URL +文件路径像这样www.mysite.com\ATTILLA\Development\some_other_folder任何建议?

回答

1

如果您希望将您的应用程序投入生产,您需要使用像nginx这样的解决方案来为您提供静态文件。 通常在开发阶段烧瓶工作与regulary静态文件(CSS,JS和其他)自我。这是正常的。 如果你想隐藏一些私人数据或上传文件,你需要使用类似这样的东西:

from flask import Flask, make_response 
... 
@app.route('/file_downloads/<filename>') 
def file_downloads(filename): 
    headers = {"Content-Disposition": "attachment; filename=%s" % filename} 
    with open('../tmp/you_private_file.zip', 'r') as f: 
     body = f.read() 
    return make_response((body, headers))