from django.shortcuts import HttpResponse
from PIL import Image
from io import BytesIO
import requests
def download_image(request):
response = requests.get('https://example.com/image.jpg')
image = Image.open(BytesIO(response.content))
response = HttpResponse(content_type='image/jpeg')
image.save(response, 'JPEG')
response['Content-Disposition'] = 'attachment; filename="image.jpg"'
return response
from django.urls import path
from . import views
urlpatterns = [
path('download-image/', views.download_image, name='download_image'),
]
<a href="{% url 'download_image' %}">Download Image</a>
When the user clicks the link, the download_image function is executed, fetching the image from the specified URL and returning it as a downloadable file named "image.jpg".
Asked: 2023-05-15 02:28:09 +0000
Seen: 16 times
Last updated: May 15 '23