我有一个 HTML 格式的图像文件,我正在尝试使用传入的不同图像(特别是最新图像)进行更新。这是烧瓶代码:@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() 被调用但 send_from_directory 似乎永远不会返回。我假设是因为 update_file 只被调用一次。知道哪里出了问题,或者这甚至是解决此问题的首选方式吗?
1 回答

桃花长相依
TA贡献1860条经验 获得超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)
这是我的文件夹结构:
这是输出:
https://i.stack.imgur.com/pQ7vx.gif
让我知道这是否适合你
添加回答
举报
0/150
提交
取消