def new(): try: # request.form only contains form input data. request.files contains file upload data. # You need to pass the combination of both to the form. form = Form_Record_Add(CombinedMultiDict( (request.files, request.form))) users = User.query.filter(User.is_active == True).all() items = Item.query.filter(Item.is_active == True).all() if request.method == 'POST': if form.validate(): # check if the post request has the file part if 'data_file_name' not in request.files: # redirect to the form page or Json response if request_wants_json(): return jsonify(data={ message: "No file part", form: form }), 422, { 'Content-Type': 'application/json' } else: flash("No file part", category="danger") return redirect(request.url) # file = request.files['data_file_name'] file = form.data_file_name.data # if user does not select file, browser also submit a empty part without filename if file.filename == '': # redirect to the form page or Json response if request_wants_json(): return jsonify(data={ message: "No selected file", form: form }), 422, { 'Content-Type': 'application/json' } else: flash("No selected file", category="danger") return redirect(request.url) if file and allowed_file(file.filename): filename = secure_filename(file.filename) filename = filename.encode('utf-8') target_dir = os.path.abspath(app.config['UPLOAD_FOLDER']) target = target_dir + '/' + filename print("------------ FILE ------------\n" + str(target)) # if target not exist if not os.path.isdir(target_dir): os.mkdir(target_dir) file.save(target) filetype = file.content_type # guess asset type asset_type = get_asset_type(filetype) if asset_type == "image": # resize if too high resize_image_to_max(target, app.config['MAX_SIZE']) filesize = os.stat(target).st_size # image processing thumbnail infilename, ext = os.path.splitext(target) filewidth = 0 fileheight = 0 if asset_type == "image": im = Image.open(target) filewidth, fileheight = im.size im.thumbnail(app.config['THUMBNAIL_SIZE']) im.save(infilename + ".thumbnail" + ext) assets = Asset() sanitize_form = { 'data_file_name': filename, 'data_content_type': filetype, 'data_file_size': filesize, 'asset_type': asset_type, 'width': filewidth, 'height': fileheight, 'description_en_US': form.description_en_US.data, 'description_fr_FR': form.description_fr_FR.data, 'user': form.user.data, 'items': form.items.data, 'is_active': form.is_active.data } assets.create_data(sanitize_form) logger.info("Adding a new record.") if request_wants_json(): return jsonify(data={ message: "Record added successfully.", form: form }), 200, { 'Content-Type': 'application/json' } else: flash("Record added successfully.", category="success") return redirect("/assets") form.action = url_for('assets_page.new') # html or Json response if request_wants_json(): return jsonify(data=form), 200, { 'Content-Type': 'application/json' } else: return render_template("assets/edit.html", form=form, users=users, items=items, title_en_US='New', app=app) except Exception, ex: print("------------ ERROR ------------\n" + str(ex.message)) flash(str(ex.message), category="warning") abort(404)