分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

latin-1 codec cant encode characters in position 42-48: ordinal not in range256 下载文件时候报错

发布时间:2023-09-06 02:10责任编辑:顾先生关键词:暂无标签

python后端写下载文件, 这个时候出现了这个错误

latin-1 codec cant encode characters in position 42-48: ordinal not in range256

怎么办:

查起因: 发现文件名有中文名字, 所以导致错误, 编码是latin-1编码, 所以我们需要解码成unicode在编码成latin-1

先看代码:

  这段代码涉及python转码问题, 一定要注意

 ???????????????FilePathName = self.get_argument("FilePathName", None) #获取文件名 ???????????????FileName = str(FilePathName.split("/")[-1]).strip() # 得到文件名 ???????????????latinFileName = FileName.encode("utf-8").decode("latin1") # 这句很关键, 要解析成latin-1才OK,这样就不会报错
class DownFileHandler(downBaseRequestHandler): ???@tornado.gen.coroutine ???def get(self, *args, **kwargs): ???????if self.verifyFlag == 1 and self.status == 0: ???????????status = 2000 ???????????try: ???????????????FilePathName = self.get_argument("FilePathName", None) ???????????????FilePathName = MyBase64.decryption(unquote(FilePathName)) # 这句是解密 ???????????????FileName = str(FilePathName.split("/")[-1]).strip() ???????????????latinFileName = FileName.encode("utf-8").decode("latin1") # 这句很关键, 要解析成latin-1才OK,这样就不会报错 ???????????????newFileName = str(int(time.time())) + "." + FileName.split(".")[1] # 第二种方式就是换一个文件名 ???????????????self.set_header("Content-Type", "application/x-zip-compressed") ???????????????# -----<或者这么写> ----- # ???????????????# self.set_header("Content-Type", "application/octet-stream") ???????????????self.set_header("Content-Disposition", "attachment; filename=%s" % latinFileName) ???????????????f = open(FilePathName, ‘rb‘) ???????????????while True: ???????????????????b = f.read(8096) ???????????????????if not b: ???????????????????????break ???????????????????else: ???????????????????????self.write(b) ???????????????self.flush() ???????????????f.close() ???????????except Exception as e: ???????????????e = FormatErrorCode(e) ???????????????my_log.error(e) ???????????????status = int(e[0]) ???????????????self.write(json.dumps(result(status=status))) ???????????self.finish() ???????else: ???????????self.write(json.dumps(result(status=4005))) ???????????self.finish()

latin-1 codec cant encode characters in position 42-48: ordinal not in range256 下载文件时候报错

原文地址:https://www.cnblogs.com/renfanzi/p/9494981.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved