1

The following code is successfully uploading an image file using the Bottle framework.

upload = bottle.request.files.get("filPhoto01")
if upload is not None:
    name, ext = os.path.splitext(upload.filename)

    if ext not in ('.png','.jpg','.jpeg'):
        return "File extension not allowed."

    save_path = "/tmp/abc".format(category=category)
    if not os.path.exists(save_path):
        os.makedirs(save_path)

    file_path = "{path}/{file}".format(path=save_path, file=upload.filename)

    with open(file_path, 'w') as open_file:
        open_file.write(upload.file.read())

However, when I try to open this file manually after upload, I can't open the file. I can see the icon of the uploaded file with the correct size (implying the whole image was uploaded), but I cannot view it in any application like MS paint, etc.

I also tried referencing the file in my web application, but it does not render there either. What could possibly be wrong?

2
  • not sure what that "/tmp/abc".format(category=category) does—it's a string constant with no placeholders, so the .format(...) call has no effect on it; also, I'd suggest using os.path.join instead of hard coding for /. Commented Sep 30, 2013 at 10:43
  • If I refrain from using the file_path variable due to the problem pointed out by you and instead use " open(upload.filename, 'w') " to test the above code it still does not solve the problem... i know this is hard coding again... but i would like to successfully execute the upload and view the file Commented Sep 30, 2013 at 11:06

1 Answer 1

1

Just a guess, but since it sounds like you're on Windows, you'll want to write the file in binary mode:

with open(file_path, 'wb') as open_file:

(Also, you didn't mention your Python version, but FYI in Python 3 you'd need to use binary mode even on Linux.)

1
  • thanx... that does help.... (my python version is 2.7 on windows) ..... the python documentation does state that binary mode may be used to make it behave consistent on all platforms irrespective of the data.... thx again Commented Oct 3, 2013 at 6:52

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.