通过django下载图像文件 [英] download image files through django
问题描述
当用户单击某些下载图像按钮时,我正在尝试从媒体文件夹下载图像
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 >>>>>>>>>>>>>>>>
/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'
所以我得到正确的图像路径,当我像localhost:8000/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 ?
更新
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/'
推荐答案
我怀疑您的媒体位于/media
文件夹中,这意味着链接localhost:8000/<path>
不在文件系统中引用文件<path>
.但是,您尝试通过url访问文件名.
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.
从Django成功交付url的事实来看,并且我猜您正在使用django.core.context_processors.media
上下文处理器交付媒体,您可以尝试在MEDIA_ROOT
之前添加到url的相对路径,删除MEDIAL_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/'
您应该在/path/to/django/media/product/product4959e24a4edf4f8d8afa8c529f3afaff/codes/image_dr_130.png
这篇关于通过django下载图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!