File could not be opened because it is empty
<p>I want to upload 2 images from requests. I am creating the function which accepts the <code>request.files</code> as a parameter and save both images.</p>

<p>The function is :</p>

<pre><code>def store_offer_images(files, **service_response):
    """
    Stores the Images
    """
    create_diretory_if_not_exist()
    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"

    sml_img.save(
        os.path.join(config.NORMAL_FILE_UPLOAD_FOLDER, sml_img_filename))
    lrg_img.save(
        os.path.join(config.LARGE_FILE_UPLOAD_FOLDER, lrg_img_filename))
</code></pre>

<p>When I am trying to save the images. It saves properly. <code>files</code> is the <code>request.files</code> and <code>service_response</code> is the <code>kwargs</code>.</p>

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

<p><code>The file “b516f2dca72e4f559c3a72a1f48727a9.png” could not be opened because it is empty.</code> </p>

<p><a href="https://i.stack.imgur.com/iM84U.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iM84U.png" alt="enter image description here"></a></p>

<p>How can I upload the images?</p>

<p><strong>Edit:</strong><br>
When I look files in the pdb, here are the response:</p>

<pre><code>(pdb) files.get('detail_image')
(pdb) &lt;FileStorage: u'python-640x444.png' ('image/png')&gt;
</code></pre>

<p>The model which models my request data is:</p>

<pre><code>import datetime
from app_config import AppConfig


class Offer(object):
    """
    Offer Details
    """

    def __init__(self, form):
        self.REST_API_TIME_FORMAT = AppConfig().REST_API_TIME_FORMAT
        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('shortDescription')
        self.long_description = form.get('longDescription')
        self.outlets = form.getlist('outlets')
        self.offer_value = form.get('offerValue')
        self.offer_old_value = form.get('oldValue')
        self.currency = form.get('currency')

    def add_offer_body(self):
        """
        Return the request body in json format
        """
        outlets_list = []
        for i in self.outlets:
            outlets_list.append({'code': i})

        starts_on_date = datetime.datetime.strptime(
            self.start_date, '%d/%m/%Y %H:%M')
        ends_on_date = datetime.datetime.strptime(
            self.end_date, '%d/%m/%Y %H:%M')

        body = {
            "outlets": outlets_list,
            "title": self.title,
            "shortDescription": self.short_description,
            "longDescription": self.long_description,
            "endsOn": ends_on_date.strftime(self.REST_API_TIME_FORMAT),
            "startsOn": starts_on_date.strftime(self.REST_API_TIME_FORMAT),
            "isActive": "true",
        }

        if self.offer_value is not u'':
            body['offerValue'] = {
                "value": self.offer_value,
                "currency": self.currency
            }

        if self.offer_old_value is not u'':
            body["offerOldValue"] = {
                "value": self.offer_old_value,
                "currency": self.currency
            }
        return body

    def __repr__(self):
        return self.title
</code></pre>

<p>The <code>views.py</code> is:</p>

<pre><code>@portal.route('/admin/offers/add', methods=['POST'])
def add_offer():
    """
    Return status of added offer
    """
    offer = Offer(request.form)
    if controller.valid_content(request.files):
        service_response = controller.add_offer(offer)
        if 'errors' in service_response:
            message = str(service_response.get('errors')[0].get('message'))
            flash("Adding Offer Failed.!! " + message)
            current_app.logger.error(
                'Offer adding failed with details' + str(request.form))
            return redirect(url_for('portal.list_running_offers'))
        else:
            controller.store_offer_images(request.files, **service_response)
            current_app.logger.info('User added a offer successfully.')
            flash("Offer added successfully..!!")
            return redirect(url_for('portal.list_running_offers'))
    else:
        flash("Please upload all Images of mentioned Resolution.!!")
        return render_template(
            'addoffer.jinja', body=offer.add_offer_body(), help=help_messages)
</code></pre>

<p>The HTML is:</p>

<pre><code>{% extends "base.jinja" %}

{% block script %}
    &lt;script type="text/javascript" src="static/js/addOffer.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript" src="static/js/bootstrap-datepicker.min.js"&gt;&lt;/script&gt;
     &lt;link href="static/css/datepicker.css" rel="stylesheet"&gt;


{% endblock %}


{% block title %}
    Add Offer
{% endblock %}


{% block content %}
    &lt;legend&gt;&lt;h1&gt;
        Add Offer
    &lt;/h1&gt;&lt;/legend&gt;
    &lt;br&gt;


    &lt;div class="row"&gt;
        &lt;div class="col-md-1"&gt;&lt;/div&gt;
        &lt;div class="col-md-10"&gt;
            &lt;form id="offerForm"enctype="multipart/form-data" class="form-horizontal" action="{{ url_for('portal.add_offer') }}" data-toggle="validator" role="form" method="post"&gt;

&lt;div class="form-group"&gt;
                &lt;label class="control-label col-xs-3" for="file"&gt;OFFER LISTING IMAGE *&lt;/label&gt;
                &lt;div class="col-xs-7 input-group"&gt;
                    &lt;input id="image" type="file" class="form-control" name="listing_image" accept="image/x-png" data-error="{{help.listing_image}}" required&gt;
                    &lt;span class="help-block with-errors"&gt;{{help.listing_image}}&lt;/span&gt;

                &lt;/div&gt;
            &lt;/div&gt;

                &lt;div class="form-group"&gt;
                    &lt;label class="control-label col-xs-3" for="file"&gt;OFFER DETAIL IMAGE *&lt;/label&gt;
                    &lt;div class="col-xs-7 input-group"&gt;
                        &lt;input id="image" type="file" class="form-control" name="detail_image" accept="image/x-png" 
                        data-error="{{help.detail_image}}" required&gt;
                        &lt;span class="help-block with-errors"&gt;{{help.detail_image}}&lt;/span&gt;

                    &lt;/div&gt;
                &lt;/div&gt;

&lt;/form&gt;
        &lt;/div&gt;
        &lt;div class="col-md-1"&gt;&lt;/div&gt;&lt;/div&gt;
    &lt;/div&gt;
{% endblock %}
</code></pre>