Issue #1058 resolved

force download

Anonymous created an issue

Hi there, I'm a complete newbe in cherrypy, anyway I've been able to create my first cherrypy web succesfully, but I need to add the possibility of dowloading a static file, always the same one. I've been reading and trying to do it by myself but no success, so I claim for help... the file I need to download is generated by a second python script. I know I have to create a download class, I don't know exactly how, nor how to call the class (or function). normal link displays it at the browser because it is a text file, that's why i need to force download. The code: {{{ def Parser(template, params): for var in params: expr = r"%"+var+"%" template = re.sub(expr, params[var], template) return template class indexpage(object): def index(self): return "NMT" = True class helloworld(object): indexpage = indexpage() _cp_config = { 'tools.staticdir.on': True, 'tools.staticdir.root': '/home/cht/Dropbox', 'tools.staticdir.dir': '', }

maybe here I showuld define the download class, something like this? (not sure)

class Download: def index(self, filepath): return serve_file(filepath, "application/x-download", "attachment") = True

def index(self):
    return """ here the html code with textarea """
def NMT(self, contents=None):
    if not contents:
        return "<a href=\"http://localhost:8080/\">Try again</a>"

here a lot of regexps like this one

        contents = re.sub(r'(C|c)([^h])', r"\1h\2" ,contents)

open (or create) the destination file

    with open("nmt.txt","w") as fileout:

the resulting html page

    return """the downloading link: <a href="nmt.txt">download</a>(or somthing similar)""" = True = True

cherrypy.config.update({'server.socket_host': '', 'server.socket_port': 8090, }) cherrypy.quickstart(helloworld()) }}}

Thanks for all your help

Reported by

Comments (2)

  1. Log in to comment