0% found this document useful (0 votes)
2 views

LAND doc

vdd

Uploaded by

lolyeat5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

LAND doc

vdd

Uploaded by

lolyeat5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

from flask import Flask, render_template, request, redirect, url_for

from werkzeug.utils import secure_filename


import os

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'static/uploads'
app.config['ALLOWED_EXTENSIONS'] = {'png', 'jpg', 'jpeg', 'gif', 'mp4', 'avi',
'mov'}

def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in
app.config['ALLOWED_EXTENSIONS']

@app.route('/', methods=['GET', 'POST'])


def upload_file():
if request.method == 'POST':
if 'file' not in request.files:
return redirect(request.url)
file = request.files['file']
if file.filename == '':
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return redirect(url_for('upload_file'))
files = os.listdir(app.config['UPLOAD_FOLDER'])
return render_template('index.html', files=files)

if __name__ == '__main__':
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
app.run(debug=True)

# Create a templates folder and add an index.html file with the following content:
"""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image and Video Upload</title>
<style>
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 1rem;
}
.grid-item {
width: 100%;
height: 200px;
object-fit: cover;
}
</style>
</head>
<body>
<h1>Upload Images and Videos</h1>
<form method="post" enctype="multipart/form-data">
<input type="file" name="file" accept="image/*,video/*">
<input type="submit" value="Upload">
</form>
<div class="grid">
{% for file in files %}
{% if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')) %}
<img src="{{ url_for('static', filename='uploads/' + file) }}"
alt="{{ file }}" class="grid-item">
{% elif file.lower().endswith(('.mp4', '.avi', '.mov')) %}
<video src="{{ url_for('static', filename='uploads/' + file) }}"
class="grid-item" controls></video>
{% endif %}
{% endfor %}
</div>
</body>
</html>

You might also like