Giter VIP home page Giter VIP logo

file_upload_in_flask's Introduction

File Upload In Flask :

Project Setup

  • Making the project as :

       mkdir file_upload_in_flask
       cd file_upload_in_flask
    
  • Install flask:

        pip install flask
    
  • Integrating SqlAlchemy

      pip install sqlalchemy
    
    • create folder structure like this :

      N|Solid

  • Declaring Models:

    from db.db import db
    import datetime
       
    class Profile(db.Model):
       id = db.Column(db.Integer, primary_key=True)
       img_name = db.Column(db.String(255), nullable=False)
       created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow, nullable=False)
    
    
  • create settings.py for configuration

    # configuration
    class Config:
       DEBUG = True
       # db
       SQLALCHEMY_DATABASE_URI = 'mysql://root:root@localhost/djangoapp'
       SQLALCHEMY_TRACK_MODIFICATIONS = False
    
  • create db.py for configuration

    from flask_sqlalchemy import SQLAlchemy
    db = SQLAlchemy()
    
  • Make a runserver configuration

    app = Flask(__name__)
    app.config.from_object('settings.Config')
    db = SQLAlchemy(app)
    
    if __name__ == "__main__":
       app.run(debug=True)
    
  • create html file inside templates folder

    • check project directory for index.html file
  • create curd def in EmployeeManagementSystem.py

    import os
    
    from flask import Flask, render_template, request, redirect, flash
    from sqlalchemy.exc import IntegrityError
    from werkzeug.utils import secure_filename
    
    from db.db import db
    from models.app_model import Profile
    
    app = Flask(__name__)
    app.secret_key = 'asrtarstaursdlarsn'
    app.config.from_object('settings.Config')
    app.config["IMAGE_UPLOADS"] = "./static/images/profiles"
    app.config["ALLOWED_IMAGE_EXTENSIONS"] = ["JPEG", "JPG", "PNG", "GIF"]
    
    # initialization
    db.init_app(app)
        
        
        def allowed_image(filename):
            if not "." in filename:
                return False
            ext = filename.rsplit(".", 1)[1]
            if ext.upper() in app.config["ALLOWED_IMAGE_EXTENSIONS"]:
                return True
            else:
                return False
        
        
        @app.route('/', methods=['GET', 'POST'])
        def upload_file():
            if request.method == 'POST':
                image = request.files["img_name"]
                if image.filename == "":
                    flash('Please Upload Image file', "danger")
                    return redirect(request.url)
                if allowed_image(image.filename):
                    filename = secure_filename(image.filename)
                    try:
                        profile_entry = Profile(img_name=filename)
                        db.session.add(profile_entry)
                        db.session.commit()
                        image.save(os.path.join(app.config["IMAGE_UPLOADS"], filename))
                        flash('File upload Successfully !', "success")
                    except IntegrityError as e:
                        flash('Something went wrong please try again later', "danger")
                        return redirect(request.url)
                else:
                    flash('That file extension is not allowed', "danger")
                    return redirect(request.url)
            profiles = Profile.query.all()
            return render_template('index.html', profiles=profiles)
        
        
        # run always put in last statement or put after all @app.route
        if __name__ == '__main__':
            app.run(host='localhost')
    
    
  • In order to run app:

      python app.py
    
  • run on your browser

file_upload_in_flask's People

Contributors

rahulmoundekar avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

praveen-jangir

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.