Python 如何在Web中使用启用的xsrf和AJAX上传多部分/表单数据
我试图通过POST HTTP请求上传文件,启用XSRF保护作为AJAX请求,但每次我都会得到:Python 如何在Web中使用启用的xsrf和AJAX上传多部分/表单数据,python,tornado,Python,Tornado,我试图通过POST HTTP请求上传文件,启用XSRF保护作为AJAX请求,但每次我都会得到: WARNING:root:403 POST /path/to/uploader/ (127.0.0.1): '_xsrf' argument missing from POST 我检查了请求的数据: _xsrf=01f86a98fe2346f9baec589dc8af3027&id=2 正如我所看到的,我将xsrf发送给处理程序,但它找不到这个参数 如果我将多部分/表单数据禁用为conte
WARNING:root:403 POST /path/to/uploader/ (127.0.0.1): '_xsrf' argument missing from POST
我检查了请求的数据:
_xsrf=01f86a98fe2346f9baec589dc8af3027&id=2
正如我所看到的,我将xsrf发送给处理程序,但它找不到这个参数
如果我将多部分/表单数据禁用为contentTypem,错误将消失,self.request.files也将为空
有人知道如何解决这个问题吗?如果包含表单的代码,可能会有所帮助 您需要在
标记(在tornado.web.RequestHandler.get()中生成)中包含此项的输出:
如果输入字段名为upload_file,则可以在处理POST的处理程序中执行此操作:
file = self.request.files['upload_file'][0]
uploaded_content_type = file['content_type']
uploaded_filename = file['filename']
local_file_path = '/some/path/on/server/uploaded.file'
output_file = open(local_file_path, 'w')
output_file.write(file['body'])
output_file.close()
如果包含表单的代码,可能会有所帮助
您需要在
标记(在tornado.web.RequestHandler.get()中生成)中包含此项的输出:
如果输入字段名为upload_file,则可以在处理POST的处理程序中执行此操作:
file = self.request.files['upload_file'][0]
uploaded_content_type = file['content_type']
uploaded_filename = file['filename']
local_file_path = '/some/path/on/server/uploaded.file'
output_file = open(local_file_path, 'w')
output_file.write(file['body'])
output_file.close()