Performance: File upload handling basics
MEDIUM IMPACT
This affects page load speed and interaction responsiveness by managing how files are received and processed on the server.
from django.core.files.storage import default_storage from django.core.files.base import ContentFile from asgiref.sync import sync_to_async from django.http import HttpResponse async def upload_file(request): if request.method == 'POST': file = request.FILES['file'] await sync_to_async(default_storage.save)(file.name, ContentFile(file.read())) return HttpResponse('File uploaded asynchronously')
def upload_file(request): if request.method == 'POST': file = request.FILES['file'] with open('/tmp/' + file.name, 'wb+') as destination: for chunk in file.chunks(): destination.write(chunk) return HttpResponse('File uploaded')
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous file save in request | Minimal | 0 | 0 | [X] Bad |
| Asynchronous file save with async views | Minimal | 0 | 0 | [OK] Good |