我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用django.core.servers.basehttp.FileWrapper()。
def get(self, request, *args, **kwargs): obj = self.get_object() if obj in request.user.myproducts.products.all(): filepath = os.path.join(settings.PROTECTED_ROOT, obj.media.path) guessed_type = guess_type(filepath)[0] wrapper = FileWrapper(file(filepath)) mimetype = 'application/force-download' if guessed_type: mimetype = guessed_type response = HttpResponse(wrapper, content_type='mimetype') if not request.GET.get("preview"): response["Content-Disposition"] = "attachment; filename=%s" %(obj.media.name) response["X-SendFile"] = str(obj.media.name) return response else: raise Http404
def src(request, app_id): myApp = get_object_or_404 (App, pk=app_id) mySources = myApp.sourcefile_set.all() temp = tempfile.TemporaryFile() arch = zipfile.ZipFile(temp, 'w') #old *working* code follows #myFileObj = io.BytesIO() #myZipFile = zipfile.ZipFile(myFileObj, 'w') for item in mySources: fname = item.file_name actualname = fname + '.java' src = (item.file_contents).encode('ascii','replace').replace("\\n", "\n") arch.writestr(actualname, src) arch.close() temp.seek(0) wrapper = FileWrapper(temp) response=HttpResponse(wrapper, content_type = "application/zip") response['Content-Disposition'] = 'attachment; filename="'+myApp.package_name+'.zip"' response ['Content-Length'] = temp.tell() return response
def apk(request, app_id): myApp = get_object_or_404 (App, pk=app_id) #TODO: Cambiar por uno que te baje el APK filepath = apk_storage.get_filepath(myApp.package_name, myApp.md5) filesize = getsize(filepath) wrapper = FileWrapper(file(filepath)) response = HttpResponse(wrapper, content_type = "application/vnd.android.package-archive") response ['Content-Disposition'] = 'attachment; filename="'+myApp.package_name+'.apk"' response ['Content-Length'] = filesize return response
def download(request): filename = os.path.join(mediafolder,'files.tar')# Select your file here. wrapper = FileWrapper(file(filename)) response = HttpResponse(wrapper, 'application/x-tar') response['Content-Length'] = os.path.getsize(filename) return response
def response_with_mimetype_and_name( mimetype, name, extension=None, show_date=True, file_path=None, use_local_filesystem=False, full_mime=False): if extension is None: extension = mimetype if not full_mime: mimetype = "application/%s" % mimetype if file_path: try: if not use_local_filesystem: default_storage = get_storage_class()() wrapper = FileWrapper(default_storage.open(file_path)) response = StreamingHttpResponse(wrapper, content_type=mimetype) response['Content-Length'] = default_storage.size(file_path) else: wrapper = FileWrapper(open(file_path)) response = StreamingHttpResponse(wrapper, content_type=mimetype) response['Content-Length'] = os.path.getsize(file_path) except IOError: response = HttpResponseNotFound( _(u"The requested file could not be found.")) else: response = HttpResponse(content_type=mimetype) response['Content-Disposition'] = disposition_ext_and_date( name, extension, show_date) return response
def email_tracking(request, emailing_id, contact_uuid): """handle download of email opening tracking image""" emailing = get_object_or_404(models.Emailing, id=emailing_id) contact = get_object_or_404(Contact, uuid=contact_uuid) emailing.opened_emails.add(contact) emailing.save() dir_name = os.path.dirname(os.path.abspath(__file__)) file_name = os.path.join(dir_name, "email-tracking.png") response = HttpResponse(FileWrapper(open(file_name, 'r')), content_type='image/png') response['Content-Length'] = os.path.getsize(file_name) return response
def zip_export(request, username, id_string): owner = get_object_or_404(User, username__iexact=username) xform = get_object_or_404(XForm, id_string__exact=id_string, user=owner) helper_auth_helper(request) if not has_permission(xform, owner, request): return HttpResponseForbidden(_(u'Not shared.')) if request.GET.get('raw'): id_string = None attachments = Attachment.objects.filter(instance__xform=xform) zip_file = None try: zip_file = create_attachments_zipfile(attachments) audit = { "xform": xform.id_string, "export_type": Export.ZIP_EXPORT } audit_log( Actions.EXPORT_CREATED, request.user, owner, _("Created ZIP export on '%(id_string)s'.") % { 'id_string': xform.id_string, }, audit, request) # log download as well audit_log( Actions.EXPORT_DOWNLOADED, request.user, owner, _("Downloaded ZIP export on '%(id_string)s'.") % { 'id_string': xform.id_string, }, audit, request) if request.GET.get('raw'): id_string = None response = response_with_mimetype_and_name('zip', id_string) response.write(FileWrapper(zip_file)) response['Content-Length'] = zip_file.tell() zip_file.seek(0) finally: zip_file and zip_file.close() return response
def zip_export(request, username, id_string): owner = get_object_or_404(User, username__iexact=username) xform = get_object_or_404(XForm, id_string__iexact=id_string, user=owner) helper_auth_helper(request) if not has_permission(xform, owner, request): return HttpResponseForbidden(_(u'Not shared.')) if request.GET.get('raw'): id_string = None attachments = Attachment.objects.filter(instance__xform=xform) zip_file = None try: zip_file = create_attachments_zipfile(attachments) audit = { "xform": xform.id_string, "export_type": Export.ZIP_EXPORT } audit_log( Actions.EXPORT_CREATED, request.user, owner, _("Created ZIP export on '%(id_string)s'.") % { 'id_string': xform.id_string, }, audit, request) # log download as well audit_log( Actions.EXPORT_DOWNLOADED, request.user, owner, _("Downloaded ZIP export on '%(id_string)s'.") % { 'id_string': xform.id_string, }, audit, request) if request.GET.get('raw'): id_string = None response = response_with_mimetype_and_name('zip', id_string) response.write(FileWrapper(zip_file)) response['Content-Length'] = zip_file.tell() zip_file.seek(0) finally: zip_file and zip_file.close() return response