如何在运行中生成文件并在下载后将其删除? [英] How can I generate file on the fly and delete it after download?

查看:144
本文介绍了如何在运行中生成文件并在下载后将其删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @ app.route(')这是我的函数,用于创建文件(当用户点击正确的链接时) / survey /< survey_id> / report')
def survey_downloadreport(survey_id):
survey,bsonobj = survey_get(survey_id)#get object
resps = response_get_multi(survey_id)对象

fields = [_id,sid,date,user_ip]#元字段
fields.extend(survey.formfields)#调查专用字段

randname =.join(random.sample(string.letters + string.digits,15))+.csv#some随机文件名

打开(csvf,fields,encoding ='cp949')
wr.writerow(dict(zip(fields, ))#dummy,解释每一列的意思是
for resp:
wr.writerow(resp)

return send_from_directory(static,randname,as_attachment = True)

我想在完成下载后删除文件。在Linux上,如果你有一个打开的文件,即使被删除,你仍然可以读取它。执行此操作:

  import tempfile $ b $ from flask import send_file 

csvf = tempfile.TemporaryFile ():
wr = csv.DictWriter(csvf,fields,encoding ='cp949')
wr.writerow(dict(字段,字段)))#dummy,解释每列的含义
for resp:
wr.writerow(resp)
wr.close()
csvf.seek(0)#回到开头

send_file(csvf,as_attachment = True,attachment_filename ='survey.csv')

csvf 文件一旦创建就会被删除;一旦文件被关闭,操作系统将回收空间(只要请求完成并且最后一个对文件对象的引用被删除,cpython会为你做这件事)。或者,您可以使用 after_this_request 挂钩显式关闭文件对象。

here's my function that creates file on the fly(when the user clicks proper link)

@app.route('/survey/<survey_id>/report')
def survey_downloadreport(survey_id):
    survey, bsonobj = survey_get(survey_id) #get object
    resps = response_get_multi(survey_id) #get responses to the object

    fields = ["_id", "sid", "date", "user_ip"] #meta-fields
    fields.extend(survey.formfields) #survey-specific fields

    randname = "".join(random.sample(string.letters + string.digits, 15)) + ".csv" #some random file name

    with open("static//" + randname, "wb") as csvf:
        wr = csv.DictWriter(csvf, fields, encoding = 'cp949')
        wr.writerow(dict(zip(fields, fields))) #dummy, to explain what each column means
        for resp in resps :
            wr.writerow(resp)

    return send_from_directory("static", randname, as_attachment = True)

I'd like to have file to be deleted after completing of the download. How can I do it?

解决方案

On Linux, if you have an open file you can still read it even when deleted. Do this:

import tempfile
from flask import send_file

csvf = tempfile.TemporaryFile():
wr = csv.DictWriter(csvf, fields, encoding = 'cp949')
wr.writerow(dict(zip(fields, fields))) #dummy, to explain what each column means
for resp in resps :
    wr.writerow(resp)
wr.close()
csvf.seek(0)  # rewind to the start

send_file(csvf, as_attachment=True, attachment_filename='survey.csv')

The csvf file is deleted as soon as it is created; the OS will reclaim the space once the file is closed (which cpython will do for you as soon as the request is completed and the last reference to the file object is deleted). Optionally, you could use the after_this_request hook to explicitly close the file object.

这篇关于如何在运行中生成文件并在下载后将其删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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