Rusfearuth
Rusfearuth

Reputation: 3261

How to upload multipart/form-data with enabled xsrf throw AJAX in tornadoweb

I'm trying to upload file by means POST HTTP request with enabled XSRF protection as AJAX request, but I get each time:

WARNING:root:403 POST /path/to/uploader/ (127.0.0.1): '_xsrf' argument missing from POST

I checked data of request:

_xsrf=01f86a98fe2346f9baec589dc8af3027&id=2

As I can see I send _xsrf to handler, but It can't find this argument.

If I disable multipart/form-data as contentTypem, bug will be gone and self.request.files is empty too.

Does anybody know how to fix that?

Upvotes: 1

Views: 639

Answers (1)

CrackerJack9
CrackerJack9

Reputation: 3651

It may help if you include the code you have for the form.

You'll need to include the output of this in your <form> tag (generated in tornado.web.RequestHandler.get()):

self.xsrf_form_html()

If the input field is named upload_file, you can do this in the handler which handles the 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()

Upvotes: 1

Related Questions