Fetch multiple images from the server using Django

Deadly 提交于 2021-01-28 06:03:14

问题


I am trying to download multiple image files from the server. I am using Django for my backend. Question related to single image has already been answered and I tried the code and it works on single image. In my application, I want to download multiple images in a single HTTP connection.

from PIL import Image

img = Image.open('test.jpg')
img2 = Image.open('test2.png')

response = HttpResponse(content_type = 'image/jpeg')
response2 = HttpResponse(content_type = 'image/png')

img.save(response, 'JPEG')
img2.save(response2, 'PNG')

return response #SINGLE

How can I fetch both img and img2 at once. One way I was thinking is to zip both images and unzip it on client size but I dont think that is good solution. Is there a way to handle this?


回答1:


I looked around and find an older solution using a temporary Zip file on disk: https://djangosnippets.org/snippets/365/

It needed some updating, and this should work (tested on django 2.0)

import tempfile, zipfile
from django.http import HttpResponse
from wsgiref.util import FileWrapper

def send_zipfile(request):
    """                                                                         
    Create a ZIP file on disk and transmit it in chunks of 8KB,                 
    without loading the whole file into memory. A similar approach can          
    be used for large dynamic PDF files.                                        
    """
    temp = tempfile.TemporaryFile()
    archive = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)
    for index in range(10):
        filename = 'C:/Users/alex1/Desktop/temp.png' # Replace by your files here.  

        archive.write(filename, 'file%d.png' % index) # 'file%d.png' will be the
                                                      # name of the file in the
                                                      # zip
    archive.close()

    temp.seek(0)
    wrapper = FileWrapper(temp)

    response = HttpResponse(wrapper, content_type='application/zip')
    response['Content-Disposition'] = 'attachment; filename=test.zip'

    return response

Right now, this takes my .png and writes it 10 times in my .zip, then sends it.




回答2:


You could add your files/images to a ZIP file and return that one in the response. I think that is the best approach.

Here is some example code of how you could achieve that (from this post):

def zipFiles(files):
    outfile = StringIO() # io.BytesIO() for python 3
    with zipfile.ZipFile(outfile, 'w') as zf:
        for n, f in enumarate(files):
            zf.writestr("{}.csv".format(n), f.getvalue())
    return outfile.getvalue()

zipped_file = zip_files(myfiles)
response = HttpResponse(zipped_file, content_type='application/octet-stream')
response['Content-Disposition'] = 'attachment; filename=my_file.zip'

Otherwise (if you don't like ZIP files) you could make individual requests from the client.



来源:https://stackoverflow.com/questions/51027868/fetch-multiple-images-from-the-server-using-django

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!