通过django下载图像文件

时间:2013-11-16 16:28:14

标签: python django image

我正在尝试在用户点击某个下载图片按钮时从媒体文件夹下载图像

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

从上面的视图我可以获得图像路径,但是得到以下错误

/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这样的图像路径时,我能够在浏览器中看到图像,那么为什么我会收到上述错误?

更新

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/'

2 个答案:

答案 0 :(得分:1)

我怀疑您的媒体位于/media文件夹中,这意味着链接localhost:8000/<path>不会引用文件系统中的文件<path>。但是,您尝试通过网址访问您的文件名。

从Django成功提供网址这一事实来看,并且我猜您正在使用django.core.context_processors.media上下文处理器来传送您的媒体,您可以尝试将MEDIA_ROOT添加到url的相对路径,从中移除MEDIAL_URL部分。

也就是说,如果你的设置是

MEDIA_ROOT = '/path/to/django/media/'
MEDIA_URL = '/media/'

您应该找到位于/path/to/django/media/product/product4959e24a4edf4f8d8afa8c529f3afaff/codes/image_dr_130.png

的文件

答案 1 :(得分:1)

以下面的方式解决

@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