我正试图弄清楚如何使用django流式传输mp3文件。我使用了中的一些代码http://djangosnippets.org/snippets/365/帮我解决这个问题。出于某种原因,下面的代码给了我一个比服务器上存储的实际文件小的文件。大小在下载窗口中显示正确,但实际文件要小得多。我尝试过使用下面的代码发送文本文件,它似乎运行得很好。我似乎搞不清楚出了什么问题。
def play_song(request, id):
song = Song.objects.get(pk=id)
# song is an object which has a FileField name file
filepath = os.path.join(MP3_STORAGE, song.file.name).replace('\', '/')
wrapper = FileWrapper(file(filepath))
response = HttpResponse(wrapper, content_type='audio/mpeg')
response['Content-Length'] = os.path.getsize(filepath.replace('/', '\'))
response['Content-Disposition'] = 'attachment; filename=%s' % song.file.name
return response
你读过关于http://djangosnippets.org/snippets/365/?尝试:
对于Windows上的用户,您需要指定";读取二进制";除了文本文件之外的任何其他文件的模式:
wrapper=FileWrapper(文件(filename);rb")
或
做了一些调整:
wrapper=FileWrapper(打开(文件名,'rb'))