python - File could not be opened because it is empty -
i want upload 2 images requests. creating function accepts request.files
parameter , save both images.
the function :
def store_offer_images(files, **service_response): """ stores 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))
when trying save images. saves properly. files
request.files
, service_response
kwargs
.
when want open image in finder(os gui window), got message:
the file “b516f2dca72e4f559c3a72a1f48727a9.png” not opened because empty.
how can upload images?
edit:
when files in pdb, here response:
(pdb) files.get('detail_image') (pdb) <filestorage: u'python-640x444.png' ('image/png')>
the model models request data is:
import datetime 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 request body in json format """ outlets_list = [] 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 not u'': body['offervalue'] = { "value": self.offer_value, "currency": self.currency } if self.offer_old_value not u'': body["offeroldvalue"] = { "value": self.offer_old_value, "currency": self.currency } return body def __repr__(self): return self.title
the views.py
is:
@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 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 offer successfully.') flash("offer added successfully..!!") return redirect(url_for('portal.list_running_offers')) else: flash("please upload images of mentioned resolution.!!") return render_template( 'addoffer.jinja', body=offer.add_offer_body(), help=help_messages)
the html is:
{% extends "base.jinja" %} {% block script %} <script type="text/javascript" src="static/js/addoffer.js"></script> <script type="text/javascript" src="static/js/bootstrap-datepicker.min.js"></script> <link href="static/css/datepicker.css" rel="stylesheet"> {% endblock %} {% block title %} add offer {% endblock %} {% block content %} <legend><h1> add offer </h1></legend> <br> <div class="row"> <div class="col-md-1"></div> <div class="col-md-10"> <form id="offerform"enctype="multipart/form-data" class="form-horizontal" action="{{ url_for('portal.add_offer') }}" data-toggle="validator" role="form" method="post"> <div class="form-group"> <label class="control-label col-xs-3" for="file">offer listing image *</label> <div class="col-xs-7 input-group"> <input id="image" type="file" class="form-control" name="listing_image" accept="image/x-png" data-error="{{help.listing_image}}" required> <span class="help-block with-errors">{{help.listing_image}}</span> </div> </div> <div class="form-group"> <label class="control-label col-xs-3" for="file">offer detail image *</label> <div class="col-xs-7 input-group"> <input id="image" type="file" class="form-control" name="detail_image" accept="image/x-png" data-error="{{help.detail_image}}" required> <span class="help-block with-errors">{{help.detail_image}}</span> </div> </div> </form> </div> <div class="col-md-1"></div></div> </div> {% endblock %}
i fixed problem. happening because if perform operation on image, move pointer @ last, when save image, start saving image pointer is, @ last. saved image empty.
now fixed move pointers beginning. here sample code did:
if img not none , img.filename != u'': img.seek(0, os.seek_end) size = img.tell() imgs = image.open(img) res = imgs.size img.seek(0)
Comments
Post a Comment