参数包含&时的编码错误

时间:2018-06-27 01:27:25

标签: python encoding cherrypy

我正在通过网络表单提交文件名,并且变得很讨厌以接收此文件名并打开以进行处理。我的问题是文件包含“ ”时,出现以下错误。 从文件名中删除“&”时,我没有问题

文件名:

  

开发票-SP30075306 Heidelberg Dust&Dropper.xlsm

Python代码:

reload(sys)  
sys.setdefaultencoding('utf8')

@cherrypy.expose
def cost_breakdown(self, **mate):
    print mate['file'].encode('UTF8')
    offer_path = os.path.join('C:/Users/rihanio/Dropbox/Projects/Python_code/work/gateway/Temp_Project_Files/temp_invoicing', mate['file'].encode('UTF8'))

错误打印输出:

  

开发票-SP30075306海德堡粉尘

     

[Errno 2]没有这样的文件或目录:   u'C:/ Users / rihanio / Dropbox / Projects / Python_code / work / gateway / Temp_Project_Files / temp_invoicing \ invoicing   -SP30075306 Heidelberg Dust'

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,方法是在发送到python之前在webform一侧编码文件名。

  

encodeURIComponent(file.name)

$.ajax({url: '/cost_breakdown?sp=&wo=&pm=' +cookie_read +'&contract=' +contract +'&file=' +encodeURIComponent(file.name),