我有一個 HTML 格式的圖像文件,我正在嘗試使用傳入的不同圖像(特別是最新圖像)進(jìn)行更新。這是燒瓶代碼:@app.route('/uploads/update_file', methods=['GET', 'POST'])def update_file(): print('update_file') list_of_files = glob.glob(staging_dir+'/*.jpg') if len(list_of_files) == 0: latest_file = '' latest_file = max(list_of_files, key=os.path.getctime) print('latest '+latest_file) splits = latest_file.split('/') file = splits[-1] return send_from_directory(directory=staging_dir, filename=file)在java腳本中我有: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> setInterval(ajaxCall,5000); function ajaxCall() { $.ajax({ type : 'GET', url : "{{url_for('update_file')}}", async: false, success: function(data) { document.getElementById("img").src = data; }, }); }</script>我看到的是 update_file() 被調(diào)用但 send_from_directory 似乎永遠(yuǎn)不會返回。我假設(shè)是因為 update_file 只被調(diào)用一次。知道哪里出了問題,或者這甚至是解決此問題的首選方式嗎?
1 回答

桃花長相依
TA貢獻(xiàn)1860條經(jīng)驗 獲得超8個贊
好的,這里是不使用 send_from_directory() 的快速解決方案,但它有效。
我基本上使用與您帖子中相同的代碼。刪除了檢查最新文件的邏輯(以保持此演示簡單)。
@app.route('/uploads/update_file', methods=['GET', 'POST'])
def update_file():
item = ["static/img/"+item for item in os.listdir("static/img")]
return random.choice(item)
這是我的文件夾結(jié)構(gòu):
這是輸出:
https://i.stack.imgur.com/pQ7vx.gif
讓我知道這是否適合你
添加回答
舉報
0/150
提交
取消