0

I am new in flask and making a small application. I uploaded some images to image/upload folder. I can able to see all the images from client side but I also want to get the uploaded image size . How can I do that ?

here is my routes.py

import os
import hashlib
import time
from PIL import Image
from file_counter_app import app, photos
from file_counter_app.form import UploadForm
from flask import render_template, request,url_for,redirect,flash


@app.route('/')
@app.route('/home')
def home():
    return render_template('home.html')


@app.route('/about')
def about():
    return render_template('about.html')


@app.route('/contact')
def contact():
    return render_template('contact.html')


@app.route('/upload_picture', methods=['GET', 'POST'])
def upload_picture():
    form = UploadForm()
    if form.validate_on_submit():
        for filename in request.files.getlist('photo'):
            str_name = 'admin'+str(int(time.time()))
            name = hashlib.md5(str_name.encode('utf-8')).hexdigest()[:15]
            photos.save(filename, name=name + '.')
            success = True
        return redirect(url_for('upload_picture'))

    return render_template('upload.html',form=form,)


@app.route('/details')
def show_details():
    files_list = os.listdir(app.config['UPLOADED_PHOTOS_DEST'])
    return render_template('details.html', files_list=(files_list))

details.html

{% extends 'layout.html' %}

{% block content %}
    <h1>File Manager</h1>
<hr>

{% for photo in files_list %}
    {{ photo }}
{% endfor %}
{% endblock %}

form.py

from file_counter_app import photos
from flask_wtf import FlaskForm
from flask_wtf.file import FileField,FileRequired,FileAllowed
from wtforms import SubmitField

#class form for image upload
class UploadForm(FlaskForm):
    photo = FileField(validators=[FileAllowed(photos,'Image Only!'),FileRequired('Choose a file')])
    submit = SubmitField('Upload')
Stevy
  • 3,228
  • 7
  • 22
  • 38
x_botz
  • 1
  • 1
  • Have you tried out other similar questions: https://stackoverflow.com/questions/15772975/flask-get-the-size-of-request-files-object – oxalorg May 25 '20 at 09:51
  • No .. Not same .. that was for uploading size .. mine is get size after upload the file – x_botz May 25 '20 at 14:04

1 Answers1

-1

Once you've got access to the value of a FileField, you've got a value of type File, which has the following methods:

File.name: The name of the file including the relative path from MEDIA_ROOT.

File.size The size of the file in bytes.

So you can do this in your template:

{{photo.file.size}}