0

I have a Tornado web application where I want to read the an uploaded file. This is received from the client and I try to do so like this:

def post(self):

    file = self.request.files['images'][0]

    dataOpen = open(file['filename'],'r');
    dataRead = dataOpen.read()

But it gives an IOError:

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\tornado\web.py", line 1332, in _execute
    result = method(*self.path_args, **self.path_kwargs)
  File "C:\Users\rsaxdsxc\workspace\pi\src\Server.py", line 4100, in post
    dataOpen = open(file['filename'],'r');
  IOError: [Errno 2] No such file or directory: u'000c02c55024aeaa96e6c79bfa2de3926dbd3767.jpg'

Why isn't it able to see the file?

Daniel Wärnå
  • 678
  • 8
  • 17
user94628
  • 3,641
  • 17
  • 51
  • 88

1 Answers1

1

Value of file['filename'] is just name of uploaded file, it is not path in your filesystem. Content of file is in file['body']. You can use StringIO module to emulate file interface if you want, or just directly iterate over file['body'].

Very good example you could use is here

So, your post request handler could look like:

def post(self):

    file = self.request.files['images'][0]
    dataRead = file['body']
    store_file_somewhere(file['filename'], dataRead)
Community
  • 1
  • 1
Dmitry Nedbaylo
  • 2,254
  • 1
  • 20
  • 20
  • Thanks, but if this an uploaded image that is going to be stored directly into a database then how would you create a path on a remote server to then save the file into the database? – user94628 Feb 04 '15 at 19:40
  • are you going to store path to image file in db or all the binary content of image file? – Dmitry Nedbaylo Feb 04 '15 at 20:52
  • I want to store straight into the DB and was able to do so with `GridFS` for mongoDB. Thanks – user94628 Feb 05 '15 at 11:25