[英]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'
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 ? 所以我得到正确的图像路径,当我进入像
localhost:8000/media/product/product4959e24a4edf4f8d8afa8c529f3afaff/codes/image_dr_130.png
的图像路径localhost:8000/media/product/product4959e24a4edf4f8d8afa8c529f3afaff/codes/image_dr_130.png
我能够在浏览器中看到图像,那么为什么我得到上述错误?
Update 更新
settings.py 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/'
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. 我怀疑你的媒体是在
/media
文件夹中,这意味着链接localhost:8000/<path>
不引用文件系统中的文件<path>
。 However, you try to access your filename via url. 但是,您尝试通过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. 从Django成功传递url的事实来看,并且我猜您正在使用
django.core.context_processors.media
上下文处理器来传递媒体,您可以尝试将MEDIA_ROOT
添加到url的相对路径, MEDIAL_URL
删除MEDIAL_URL
部分。
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
你应该找到你的文件位于
/path/to/django/media/product/product4959e24a4edf4f8d8afa8c529f3afaff/codes/image_dr_130.png
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.