PythonAnywhere中部署文件上传网站,将上传的文件到网站中,保存在数据库,当访问url时,要求输入固定口令666即可访问文件目录选择要下载的文件,点击即可下载,完成这个项目需要添加几个文件,每个文件的代码是多少,PythonAnywhere中部署文件上传网站,将上传的文件到网站中,保存在数据库,当访问url时,要求输入固定口令666即可访问文件目录选择要下载的文件,点击即可下载,完成这个项目需要添加几个文件,每个文件的代码是多少,Source code:选择哪个文件,Working directory:选择哪个文件
时间: 2024-03-22 15:39:07 浏览: 148
这是一个比较复杂的项目,需要添加多个文件,并进行文件上传、保存、下载等操作。以下是需要添加的文件和其代码:
1. app.py:这是Flask应用程序的主要文件,包含了路由、视图函数等。以下是代码示例:
```
from flask import Flask, render_template, request, redirect, url_for, send_from_directory
import os
from werkzeug.utils import secure_filename
import sqlite3
app = Flask(__name__)
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
DATABASE = 'files.db'
def get_db():
db = sqlite3.connect(DATABASE)
db.row_factory = sqlite3.Row
return db
def init_db():
db = get_db()
with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()
@app.route('/')
def home():
return render_template('home.html')
@app.route('/upload', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
db = get_db()
db.execute('INSERT INTO files (filename) VALUES (?)', (filename,))
db.commit()
return redirect(url_for('home'))
return render_template('upload.html')
@app.route('/download', methods=['GET', 'POST'])
def download():
if request.method == 'POST':
filename = request.form['filename']
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
return send_from_directory(app.config['UPLOAD_FOLDER'], filename, as_attachment=True)
db = get_db()
files = db.execute('SELECT filename FROM files').fetchall()
return render_template('download.html', files=files)
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
if __name__ == '__main__':
app.run()
```
2. schema.sql:这是用于创建数据库表的SQL文件,代码如下:
```
DROP TABLE IF EXISTS files;
CREATE TABLE files (
id INTEGER PRIMARY KEY AUTOINCREMENT,
filename TEXT NOT NULL
);
```
3. templates/home.html:这是主页模板,代码如下:
```
<!DOCTYPE html>
<html>
<head>
<title>File Upload App</title>
</head>
<body>
<h1>Welcome to the File Upload App</h1>
<p>Use the links below to upload or download files:</p>
<ul>
<li><a href="{{ url_for('upload') }}">Upload a File</a></li>
<li><a href="{{ url_for('download') }}">Download a File</a></li>
</ul>
</body>
</html>
```
4. templates/upload.html:这是上传页面模板,代码如下:
```
<!DOCTYPE html>
<html>
<head>
<title>File Upload App</title>
</head>
<body>
<h1>Upload a File</h1>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file" required>
<button type="submit">Upload</button>
</form>
</body>
</html>
```
5. templates/download.html:这是下载页面模板,代码如下:
```
<!DOCTYPE html>
<html>
<head>
<title>File Upload App</title>
</head>
<body>
<h1>Download a File</h1>
{% if files %}
<form method="POST">
<select name="filename" required>
{% for file in files %}
<option value="{{ file['filename'] }}">{{ file['filename'] }}</option>
{% endfor %}
</select>
<button type="submit">Download</button>
</form>
{% else %}
<p>No files found.</p>
{% endif %}
</body>
</html>
```
6. static/style.css:这是用于渲染HTML页面的CSS文件,代码如下:
```
body {
font-family: Arial, sans-serif;
font-size: 14px;
}
h1 {
margin-top: 20px;
margin-bottom: 20px;
}
form {
margin-top: 20px;
}
ul {
list-style: none;
padding: 0;
}
li {
margin-bottom: 10px;
}
select {
width: 200px;
margin-right: 10px;
}
```
这些文件需要添加到PythonAnywhere中,并进行配置,其中:
- Source code:选择app.py、schema.sql、templates、static文件夹;
- Working directory:选择包含这些文件的目录。
阅读全文