Home Ask Login Register

Developers Planet

Your answer is one click away!

Rajat Gupta February 2016

File could not be opened because it is empty

I want to upload 2 images from requests. I am creating the function which accepts the request.files as a parameter and save both images.

The function is :

def store_offer_images(files, **service_response):
    Stores the Images
    lrg_img, sml_img = files.get('detail_image'), files.get('listing_image')

    lrg_img_filename = service_response.get('content').get('offerId') + ".png"
    sml_img_filename = service_response.get('content').get('offerId') + ".png"

        os.path.join(config.NORMAL_FILE_UPLOAD_FOLDER, sml_img_filename))
        os.path.join(config.LARGE_FILE_UPLOAD_FOLDER, lrg_img_filename))

When I am trying to save the images. It saves properly. files is the request.files and service_response is the kwargs.

When I want to open image in Finder(OS GUI Window), then I got the message:

The file “b516f2dca72e4f559c3a72a1f48727a9.png” could not be opened because it is empty.

enter image description here

How can I upload the images?

When I look files in the pdb, here are the response:

(pdb) files.get('detail_image')
(pdb) <FileStorage: u'python-640x444.png' ('image/png')>

The model which models my request data is:

import datetime
from app_config import AppConfig

class Offer(object):
    Offer Details

    def __init__(self, form):
        self.title = form.get('title')
        self.start_date = form.get('startsOn') + ' 00:00'
        self.end_date = form.get('endsOn') + ' 23:59'
        self.short_description = form.get('shortD        


Rajat Gupta March 2016

I fixed the problem. It was happening because If I perform any operation on Image, it will move my pointer at last, so when you save the image, it start saving image where your pointer is, which is at the last. So the saved image is empty.

Now to fixed this I just move my pointers to the beginning. Here is sample code what I did:

if img is not None and img.filename != u'':
            img.seek(0, os.SEEK_END)
            size = img.tell()
            imgs = Image.open(img)
            res = imgs.size

Post Status

Asked in February 2016
Viewed 1,623 times
Voted 8
Answered 1 times


Leave an answer

Quote of the day: live life