This content originally appeared on DEV Community and was authored by Nelson Adonis Hernandez
In this example I will show you how to upload, download, delete and obtain files with Flask
How to upload multiple files by Form Data using Flask
from flask import Flask
import os
app = Flask(__name__)
@app.route("/upload", methods=['POST'])
def upload_image():
if request.method == "POST":
file = request.files['file']
try:
file.save(os.getcwd() + "/images/" + file.filename)
return "Imagen saved"
except FileNotFoundError:
return "Folder not found"
if __name__ == '__main__':
app.run(debug=True, port=8000, host="0.0.0.0")
How to download files using Flask
from flask import Flask
import os
app = Flask(__name__)
@app.route('/download/file/<string:filename>')
def download_image(filename):
return send_from_directory(os.getcwd() + "/images/", path=filename, as_attachment=True)
if __name__ == '__main__':
app.run(debug=True, port=8000, host="0.0.0.0")
How to get files using Flask
from flask import Flask
import os
app = Flask(__name__)
@app.route('/file/<string:filename>')
def get_image(filename):
return send_from_directory(os.getcwd() + "/images/", path=filename, as_attachment=False)
if __name__ == '__main__':
app.run(debug=True, port=8000, host="0.0.0.0")
How to delete files using Flask with Form Data
from flask import Flask
import os
app = Flask(__name__)
@app.route('/delete', methods=['POST'])
def remove_image():
filename = request.form['filename']
# CHECK IF EXISTS FILE
if os.path.isfile(os.getcwd() + "/images/" + filename) == False:
return "Esto no es un archivo"
else:
try:
os.remove(os.getcwd() + "/images/" + filename)
except OSError:
return "Error :(("
return "File deleted"
if __name__ == '__main__':
app.run(debug=True, port=8000, host="0.0.0.0")
This content originally appeared on DEV Community and was authored by Nelson Adonis Hernandez
Print
Share
Comment
Cite
Upload
Translate
Updates
There are no updates yet.
Click the Upload button above to add an update.

APA
MLA
Nelson Adonis Hernandez | Sciencx (2021-08-03T23:38:39+00:00) How to create server files with Flask. Retrieved from https://www.scien.cx/2021/08/03/how-to-create-server-files-with-flask/
" » How to create server files with Flask." Nelson Adonis Hernandez | Sciencx - Tuesday August 3, 2021, https://www.scien.cx/2021/08/03/how-to-create-server-files-with-flask/
HARVARDNelson Adonis Hernandez | Sciencx Tuesday August 3, 2021 » How to create server files with Flask., viewed ,<https://www.scien.cx/2021/08/03/how-to-create-server-files-with-flask/>
VANCOUVERNelson Adonis Hernandez | Sciencx - » How to create server files with Flask. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/03/how-to-create-server-files-with-flask/
CHICAGO" » How to create server files with Flask." Nelson Adonis Hernandez | Sciencx - Accessed . https://www.scien.cx/2021/08/03/how-to-create-server-files-with-flask/
IEEE" » How to create server files with Flask." Nelson Adonis Hernandez | Sciencx [Online]. Available: https://www.scien.cx/2021/08/03/how-to-create-server-files-with-flask/. [Accessed: ]
rf:citation » How to create server files with Flask | Nelson Adonis Hernandez | Sciencx | https://www.scien.cx/2021/08/03/how-to-create-server-files-with-flask/ |
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.