我正在通过网络表单提交文件名,并且变得很讨厌以接收此文件名并打开以进行处理。我的问题是文件包含“ &”时,出现以下错误。 从文件名中删除“&”时,我没有问题
文件名:
开发票-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'
答案 0 :(得分:0)
我已经解决了这个问题,方法是在发送到python之前在webform一侧编码文件名。
encodeURIComponent(file.name)
$.ajax({url: '/cost_breakdown?sp=&wo=&pm=' +cookie_read +'&contract=' +contract +'&file=' +encodeURIComponent(file.name),