我的django view.py:
def get(self, request, *args, **kwargs):
try:
self.object = self.get_object()
file_path = settings.MEDIA_ROOT+self.object.file.name
chunk_size = 8192
response = StreamingHttpResponse(FileWrapper(open(file_path), chunk_size),
content_type=mimetypes.guess_type(file_path)[0])
response['Content-Length'] = os.stat(file_path).st_size
response['Content-Disposition'] = 'attachment; filename="%s"' % smart_str(os.path.basename( file_path ))
print 'response:', response
return response
响应(来自日志文件):
[Wed Aug 03 14:16:54 2016] [error] response: Content-Length: 4194486\r
[Wed Aug 03 14:16:54 2016] [error] Content-Type: image/x-ms-bmp\r
[Wed Aug 03 14:16:54 2016] [error] Content-Disposition: attachment; filename="TestFileName`~@#$%^&*()-_+={}[]|:;<>,.? test '.bmp
但是文件名中的一些字符似乎在下载时已被替换。
这是浏览器的行为?欢迎任何评论。感谢