Flask-下载后删除文件 [英] Flask - delete file after download

查看:58
本文介绍了Flask-下载后删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码需要一些帮助.在我的网站上,我无法弄清楚如何解决问题.我通过JavaScript解释了代码,该代码创建了一个链接,该链接可让您下载静态文件夹所请求的文档.这样做.

I need some help with my code. In my site I am not able to figure out how to solve a problem. I explain the code via JavaScript creates a link that lets you download the document requested by the static folder. Doing so.

@ App.route ( '/ static / document / <path: name>', methods = [ 'POST'])
def downloads (name): #name is the name of the document
    return os.remove (name)

然后我拿走该文件,但该文件未删除.这是下载此文件的javascript代码.

Then the document I take it, but the file is not deleted. This is the javascript code for download this file.

downloadlink var = document.createElement ( "a");
                        d = obj.d; # D is download method before
                        downloadlink.href = d;
                        downloadlink.className = "DOWNLOAD_LINK";
                        downloadlink.download = n;
                        downloadlink.onClick = setTimeout (function () {location.reload (true);}, 30000);
                        downloadlink.innerHTML = "<p> Download document" + n + "</ p>";
                        document.getElementById ( "results"). appendChild (downloadlink);

我在哪里错了?

推荐答案

此代码已解决.

@app.route('/path/<name>') 
def download(name): 
   file_path ="/path/"+name
   file_handle = open(file_path, 'r')

   @after_this_request 
   def remove_file(response): 
      os.remove("/path/"+name) 
      return response 

   return send_file(file_handle)

这篇关于Flask-下载后删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆