1
我嘗試從我的django網站下載圖像。我這樣做:通過django下載圖像
def file_download(request, filename):
from django.core.servers.basehttp import FileWrapper
import mimetypes
import settings
import os
filepath = os.path.join(settings.MEDIA_ROOT, filename)
wrapper = FileWrapper(open(filepath))
content_type = mimetypes.guess_type(filepath)[0]
response = HttpResponse(wrapper, mimetype='content_type')
response['Content-Disposition'] = "attachment; filename=%s" % filename
return response
然而,它不適用於圖像(我試圖jpg文件),但爲txt文件工作。爲什麼?
是的,它的作品!我可以知道什麼時候應該以二進制模式打開文件嗎? – ChanDon
當他們不是文字,基本上。 –