URL通配符示例:

url(r'^file_download/(?P<filename>(.)*)$', views.FILE_DOWNLOAD_VIEW.as_view()),

 

代码示例:

    def get(self, request, filename):
        from common.s3_storage import S3_STORAGE
        s3_storage = S3_STORAGE()
        key = s3_storage.package_bucket.get_key(filename)
        assert(key is not None)
        
        import tempfile
        temp = tempfile.NamedTemporaryFile()
        try:
            #temp = tempfile.TemporaryFile()
            key.get_contents_to_filename(temp.name)
            temp.seek(0)
            c = temp.read()
            
            #c = key.get_contents_as_string()
            #return APIResponse(status=status_code.success, data=OS_AND_KERNEL_MAPS)
            from django.http import StreamingHttpResponse
            #response = HttpResponse(c)
            response = StreamingHttpResponse(c)
            #response = StreamingHttpResponse( self.file_iterator(temp) )
            
            response['Content-Type'] = 'application/octet-stream'
            import os
            response['Content-Length'] = os.path.getsize(temp.name)
            response['Content-Disposition'] = 'attachment;filename="{0}"'.format(filename)
            return response
        finally:
            temp.close()

 

相关文章:

  • 2021-06-11
  • 2021-10-25
  • 2022-12-23
  • 2021-12-16
  • 2022-01-01
猜你喜欢
  • 2021-12-22
  • 2021-11-24
  • 2021-09-08
  • 2021-05-28
  • 2021-08-03
  • 2021-10-16
  • 2021-11-13
相关资源
相似解决方案