attachment filename 中文名浏览器乱码

2018-03-30  本文已影响0人  Caca哒

python3.6+ tornado

支持中文,()

浏览器乱码支持

        
        userAgent = req_headers['User-Agent']
        # IE浏览器 
        if 'MSIE' in userAgent or 'Trident' in userAgent or 'Edge' in userAgent:
            output_file_name = parse.quote(output_file_name)
        #其他浏览器
        else:
            output_file_name = output_file_name.encode('utf-8').decode('ISO-8859-1')
            
        self.set_header('Content-Type', 'application/octet-stream')
        self.set_header(
            'Content-Disposition', f"attachment; filename=\"{output_file_name}\"")
        with open(output_file_path, 'rb') as fp:
            data = fp.read()
            return self.finish(data)

上一篇下一篇

猜你喜欢

热点阅读