2017-08-11 79 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))