Skip to main content
 首页 » 编程设计

python之Tornado Web 服务器文件下载的问题

2024年08月29日45JustinYoung

Tornado 网络服务器。

我有一个问题。

如何通过 Tornado Web 服务器下载文件?

这是我的代码。

def get(self): 
    data = self.xls_f() 
    self.set_header('Content-Type', 'application/apk') 
    self.set_header('Content-Disposition', 'attachment; filename=%s' % 'test.apk') 
    f = file('test.apk')  
    self.write(f.read()) 
    self.finish() 
  1. 我得到的文件包含不可读的内容,例如“asdfafasfd ㅁ ㅁ ㅇㄴㅁㄹㅁㄹㄴ” 也许 this('\n') 不可读。

  2. apk、图像文件等我可以下载,但文件大小为 0k 或 1k。

我怎样才能将多种文件(图片、apk 等)下载到 tornado 网络服务器?

我想示例源代码。请。

请您参考如下方法:

下面的代码用于从 tornado 服务器下载文件,我在我的应用程序中使用它来下载 csv 文件。它可能对您有所帮助。

def post(self,filename): 
    print('i download file handler : ',filename) 
 
    ifile  = open(filename+".csv", "r") 
    self.set_header ('Content-Type', 'text/csv') 
    self.set_header ('Content-Disposition', 'attachment; filename='+filename+'') 
    self.write (ifile.read())