Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Web中使用启用的xsrf和AJAX上传多部分/表单数据_Python_Tornado - Fatal编程技术网

Python 如何在Web中使用启用的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

我试图通过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发送给处理程序,但它找不到这个参数

如果我将多部分/表单数据禁用为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()