dimanche 8 janvier 2017

Python2.7 Cherrypy File download not working

I am building a very simple mobile app type page for download apk file using cherrypy with index.html

In current directory i have test.py and server.conf files:

test.py

#!/usr/bin/env python

import cherrypy
from cherrypy.lib.static import serve_file
from os.path import abspath
import os
import os.path

class Main_Page:
    def index(self):
        return file("media/index.html")
    index.exposed = True

    def download(self, path="media/test.apk"):
        return serve_file(path, "application/x-download", "attachment", debug=False)
    download.exposed = True

CP_CONF = {
        '/media': {
            'tools.staticdir.on': True,
            'tools.staticdir.dir': abspath('./media') # staticdir needs an absolute path
            }
        }

if __name__ == '__main__':
    cherrypy.config.update('server.conf')
    cherrypy.quickstart(Main_Page(),'/',CP_CONF)

server.conf:

[global]
environment = "production"
log.screen = True
server.socket_host = "0.0.0.0"
server.socket_port = 8080
server.thread_pool = 5
engine.autoreload_on = True
log.error_fie = "errors.log"
log.access_file = "access.log"

i have made another sub-directory within current directory with folder name > "media". In media folder there is 3 files. index.html, style.css and test.apk

media/index.html

<html>
<head>
<link rel="stylesheet" type="text/css" href="media/style.css">
</head>

<body>
This is the body test page.

<div class="col-xs-12 col-sm-5">
<div class="clearfix button-holder">
<a id="download-new" class="button download pull-right download-new" style="display: block;" href="./media/test.apk"><i class="sprite sprite-download"></i>Download</a>
</div></div>

</body>

</html>

All code is working fine. It loads style.css file and index.html file with download button correctly from media directory. But when i click on "Download" Button then it show me test.apk code on web-browser, instead of download test.apk file. I want to download the file test.apk when user click on download button. Could some one help me please where i am going wrong?




Aucun commentaire:

Enregistrer un commentaire