简体   繁体   English

Flask-下载后删除文件

[英]Flask - delete file after download

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. 我通过JavaScript解释了代码,创建了一个链接,该链接可让您下载静态文件夹所请求的文档。 Doing so. 这样做。

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

Then the document I take it, but the file is not deleted. 然后我拿走了文件,但是文件没有被删除。 This is the javascript code for download this file. 这是下载此文件的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);

where am I wrong? 我哪里错了?

Resolved with this code. 用此代码解决。

@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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM