├── .gitignore ├── LICENSE ├── README.md ├── requirements.txt ├── server.py └── test.html /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Phakin Cheangkrachange 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # flask-api-upload-image 2 | Flask API upload image and save to file. 3 | 4 | ## Setup 5 | ``` 6 | pip install -r requirements.txt 7 | ``` 8 | 9 | ## Start server 10 | ``` 11 | python server.py 12 | ``` -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | click==6.7 2 | Flask==0.12.2 3 | Flask-API==1.0 4 | itsdangerous==0.24 5 | Jinja2==2.9.6 6 | MarkupSafe==1.0 7 | Werkzeug==0.12.2 8 | -------------------------------------------------------------------------------- /server.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, url_for, send_from_directory, request 2 | import logging, os 3 | from werkzeug import secure_filename 4 | 5 | app = Flask(__name__) 6 | file_handler = logging.FileHandler('server.log') 7 | app.logger.addHandler(file_handler) 8 | app.logger.setLevel(logging.INFO) 9 | 10 | PROJECT_HOME = os.path.dirname(os.path.realpath(__file__)) 11 | UPLOAD_FOLDER = '{}/uploads/'.format(PROJECT_HOME) 12 | app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER 13 | 14 | 15 | def create_new_folder(local_dir): 16 | newpath = local_dir 17 | if not os.path.exists(newpath): 18 | os.makedirs(newpath) 19 | return newpath 20 | 21 | @app.route('/', methods = ['POST']) 22 | def api_root(): 23 | app.logger.info(PROJECT_HOME) 24 | if request.method == 'POST' and request.files['image']: 25 | app.logger.info(app.config['UPLOAD_FOLDER']) 26 | img = request.files['image'] 27 | img_name = secure_filename(img.filename) 28 | create_new_folder(app.config['UPLOAD_FOLDER']) 29 | saved_path = os.path.join(app.config['UPLOAD_FOLDER'], img_name) 30 | app.logger.info("saving {}".format(saved_path)) 31 | img.save(saved_path) 32 | return send_from_directory(app.config['UPLOAD_FOLDER'],img_name, as_attachment=True) 33 | else: 34 | return "Where is the image?" 35 | 36 | if __name__ == '__main__': 37 | app.run(host='0.0.0.0', debug=False) -------------------------------------------------------------------------------- /test.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Test Dense Server 4 | 5 | 6 |
7 | file :
8 |
9 |
10 | 11 | --------------------------------------------------------------------------------