簡體   English   中英

無法提供 Static 文件

[英]Cannot Serve Static Files

目前,在我的靜態文件服務路由上向我的 API 發送一個 postman 請求會給我一個 404 錯誤,盡管我仔細檢查了我從正確的路由獲取。 在添加 SocketIO 之前,服務器在提供文件方面運行良好。 不幸的是,即使我刪除了 flask-socketio 中間件,我也無法獲取要提供的文件。 目前,我已經很久沒有接觸過這條路線了,所以我不確定到底發生了什么。

以前,我在啟動 flask 應用程序 object 時沒有設置 static_url_path 和 static_folder 選項。 我只是在閱讀了其他一些答案后才添加它們,但無論它們是否存在,我都會得到 404。

我所有的其他路線都按預期工作。

UPLOAD_FOLDER = '/profile_pictures'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
app = flask.Flask(__name__, static_url_path='/', static_folder="/profile_pictures")

app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

UPLOAD_FOLDER = './profile_pictures'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}

#unrelated code

@app.route('/profile_pictures/<filename>', methods=['GET'])
@cross_origin(supports_credentials=True)
def get_profile_picture(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'],filename,  as_attachment=True)

非常感謝您對問題的任何見解。

static 文件不需要static_url_path='/', static_folder="/profile_pictures"route

選擇您喜歡使用的一個並刪除另一個。

或更改

static_url_path='/', static_folder="/profile_pictures"

進入

static_url_path='/static', static_folder="/profile_pictures"

和 static 文件可以從兩者訪問

HOST:PORT/static/<filename>HOST:PORT/profile_pictures/<filename>

static_folder基本上用於 static 文件,例如cssjsimages route供用戶直接下載文件。

最終我將 UPLOAD_FOLDER 目錄設置為

UPLOAD_FOLDER = '/profile_pictures'

它應該在哪里

UPLOAD_FOLDER = 'profile_pictures'

感謝@亞歷山大的回答:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM