您好,登錄后才能下訂單哦!
Flask和Django都是Python Web框架,它們都支持文件上傳和下載處理。下面分別介紹它們在文件上傳和下載方面的實現方法。
Flask是一個輕量級的Web框架,使用Flask處理文件上傳和下載相對簡單。
上傳文件:
首先,安裝Flask-Upload庫:
pip install Flask-Upload
然后,創建一個簡單的Flask應用來處理文件上傳:
from flask import Flask, render_template, request
from flask_uploads import UploadSet, configure_uploads, IMAGES, patch_request_class
app = Flask(__name__)
app.config['UPLOADED_IMAGES_DEST'] = 'static/uploads'
upload_set = UploadSet('images', IMAGES)
configure_uploads(app, upload_set)
patch_request_class(app)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload/', methods=['POST'])
def upload():
file = request.files['file']
if file and file.filename != '':
img = upload_set.save(file)
return f'File {img} uploaded successfully.'
else:
return 'No file selected.'
if __name__ == '__main__':
app.run(debug=True)
創建一個index.html
模板文件,用于上傳文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>File Upload</title>
</head>
<body>
<h1>Upload a File</h1>
<form action="/upload/" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
</body>
</html>
下載文件:
在Flask中,可以使用send_file
函數來下載文件:
from flask import send_file
@app.route('/download/<filename>')
def download(filename):
return send_file(os.path.join('static/uploads', filename), as_attachment=True)
Django是一個高級的Web框架,它提供了許多內置功能來處理文件上傳和下載。
上傳文件:
首先,在settings.py
中配置文件上傳相關設置:
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
然后,在models.py
中定義一個模型來存儲上傳的文件:
from django.db import models
class File(models.Model):
file = models.FileField(upload_to='uploads/')
uploaded_at = models.DateTimeField(auto_now_add=True)
接下來,創建一個表單來處理文件上傳:
from django import forms
from .models import File
class FileUploadForm(forms.ModelForm):
class Meta:
model = File
fields = ['file']
在views.py
中處理文件上傳:
from django.shortcuts import render, redirect
from .forms import FileUploadForm
def upload_file(request):
if request.method == 'POST':
form = FileUploadForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('success')
else:
form = FileUploadForm()
return render(request, 'upload.html', {'form': form})
創建一個upload.html
模板文件,用于上傳文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>File Upload</title>
</head>
<body>
<h1>Upload a File</h1>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Upload">
</form>
</body>
</html>
下載文件:
在views.py
中處理文件下載:
def download_file(request, filename):
file = File.objects.get(filename=filename)
return FileResponse(open(file.file.path, 'rb'), content_type='application/octet-stream')
最后,在urls.py
中添加URL路由:
from django.urls import path
from . import views
urlpatterns = [
path('upload/', views.upload_file, name='upload_file'),
path('download/<str:filename>/', views.download_file, name='download_file'),
]
這樣,你就可以使用Django處理文件上傳和下載了。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。