樱桃py自动下载文件 [英] cherry py auto download file

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

问题描述

我目前正在为我的项目构建Cherry py应用程序,并且在某些功能下,我需要自动开始下载文件.

I'm currently building cherry py app for my projects and at certain function I need auto starting download a file.

zip文件生成完毕后,我要开始下载到客户端因此,在创建图像后,将它们压缩并发送给客户端

After zip file finish generating, I want to start downloading to client So after images are created, they are zipped and sent to client

class Process(object):
    exposed = True

    def GET(self, id, norm_all=True, format_ramp=None):
        ...
        def content(): #generating images
            ...

            def zipdir(basedir, archivename):
                assert os.path.isdir(basedir)
                with closing(ZipFile(archivename, "w", ZIP_DEFLATED)) as z:
                    for root, dirs, files in os.walk(basedir):
                        #NOTE: ignore empty directories
                        for fn in files:
                            absfn = os.path.join(root, fn)
                            zfn = absfn[len(basedir)+len(os.sep):] #XXX: relative path
                            z.write(absfn, zfn)

            zipdir("/data/images/8","8.zip")

            #after zip file finish generating, I want to start downloading to client
            #so after images are created, they are zipped and sent to client
            #and I'm thinking do it here, but don't know how

        return content()

    GET._cp_config = {'response.stream': True}


    def POST(self):
        global proc
        global processing
        proc.kill()
        processing = False

推荐答案

只需在内存中创建一个zip存档,然后使用 cherrypy.lib 中的 file_generator()帮助函数将其返回即可.代码>.您也可以通过 yield HTTP响应来启用流功能(请记住在执行此操作之前先设置HTTP标头).我为您编写了一个简单的示例(基于您的代码段),您只需返回一个完整的缓冲zip归档文件即可.

Just create a zip archive in memory and then return it using file_generator() helper function from cherrypy.lib. You may as well yield HTTP response to enable streaming capabilities (keep in mind to set HTTP headers prior to doing that). I wrote a simple example (based on your snippet) for you just returning a whole buffered zip archive.

from io import BytesIO

import cherrypy
from cherrypy.lib import file_generator


class GenerateZip:
    @cherrypy.expose
    def archive(self, filename):
        zip_archive = BytesIO()
        with closed(ZipFile(zip_archive, "w", ZIP_DEFLATED)) as z:
            for root, dirs, files in os.walk(basedir):
                #NOTE: ignore empty directories
                for fn in files:
                    absfn = os.path.join(root, fn)
                    zfn = absfn[len(basedir)+len(os.sep):] #XXX: relative path
                    z.write(absfn, zfn)


        cherrypy.response.headers['Content-Type'] = (
            'application/zip'
        )
        cherrypy.response.headers['Content-Disposition'] = (
            'attachment; filename={fname}.zip'.format(
                fname=filename
            )
        )

        return file_generator(zip_archive)

我没有测试这段特定的代码,但是总体思路是正确的.

N.B. I didn't test this specific piece of code, but the general idea is correct.

这篇关于樱桃py自动下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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