烧瓶 - 下载后删除文件

Flask - delete file after download

本文关键字:删除 文件 下载 烧瓶      更新时间:2023-09-26

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

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

然后文档我拿走了它,但文件没有被删除。这是下载此文件的 javascript 代码。

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)