Reputation: 26738
I am trying to download images from media folder when a user clicks on some download image button
from django.core.servers.basehttp import FileWrapper
import mimetypes
import settings
import os
@login_required
def download_image(request, product_id):
# current_site = get_current_site(request)
product_image = Product.objects.get(object_id=product_id)
product_image_url = product_image.product_image_url()
print product_image_url,">>>>>>>>>>>>>>"
wrapper = FileWrapper(open(product_image_url, 'rb'))
print wrapper,">>>>>>>>>>>>>>>>>"
content_type = mimetypes.guess_type(product_image_url)[0]
response = HttpResponse(wrapper, mimetype=content_type)
response['Content-Disposition'] = "attachment; filename=%s" % product_image_url
return response
From the above view i can able to get the image path, but getting the following error
/media/product/product4959e24a4edf4f8d8afa8c529f3afaff/codes/image_dr_130.png >>>>>>>>>>>>>>>>
Traceback (most recent call last):
File "/home/user/Envs/comp/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/home/user/Envs/comp/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 25, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/user/name/cpmp/comp/projsite/apps/website/views.py", line 963, in download_image
wrapper = FileWrapper(open(product_image_url, 'rb'))
IOError: [Errno 2] No such file or directory: '/media/product/product4959e24a4edf4f8d8afa8c529f3afaff/codes/image_dr_130.png'
so i am getting the correct image path,and when i entered the image path like localhost:8000/media/product/product4959e24a4edf4f8d8afa8c529f3afaff/codes/image_dr_130.png
i am able to see the image in browser, so why am i getting the above error ?
Update
settings.py
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
def ABS_PATH(*args):
return os.path.join(ROOT_DIR, *args)
# Example: "/home/media/media.lawrence.com/media/"
MEDIA_ROOT = ABS_PATH('media')
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/"
MEDIA_URL = '/media/'
Upvotes: 0
Views: 3163
Reputation: 26738
Solved in the below manner
@login_required
def download_image(request, product_id):
# current_site = get_current_site(request)
product_image = Product.objects.get(object_id=product_id)
product_image_url = product_image.product_image_url()
wrapper = FileWrapper(open(settings.MEDIA_ROOT+ product_image_url[6:], 'rb'))
content_type = mimetypes.guess_type(product_image_url)[0]
response = HttpResponse(wrapper, mimetype=content_type)
response['Content-Disposition'] = "attachment; filename=%s" % product_image_url
return response
Upvotes: 1
Reputation: 48357
I doubt that your media is in /media
folder, that means link localhost:8000/<path>
do not refer to file <path>
in your file system. However, you try to access your filename via url.
Judging from the fact that Django succesfully delivers the url, and as I guess you're using django.core.context_processors.media
context processor to deliver your media, you can try to prepend MEDIA_ROOT
to relative path to url, removing MEDIAL_URL
part from it.
That is, if your settings are
MEDIA_ROOT = '/path/to/django/media/'
MEDIA_URL = '/media/'
you should find your file located at /path/to/django/media/product/product4959e24a4edf4f8d8afa8c529f3afaff/codes/image_dr_130.png
Upvotes: 1