Skip to content Skip to sidebar Skip to footer

Not Able To Serve Mp3 Files In Django

I am trying to serve MP3 files to django templates which can be used in audio tag. I am using the following view. def get_file(request): filename = FILE_PATH + '\\' + files['k'

Solution 1:

You'll need to open the MP3 file in binary mode:

wrapper = FileWrapper(open(filename, 'rb'))

If you open the file in textmode (the default) then various line endings are normalized to \n, which is great for text, but in binary information such as an MP3 file that's a big problem.

Note that I use the open function here, not the file constructor; from the file documentation:

When opening a file, it’s preferable to use open() instead of invoking this constructor directly. file is more suited to type testing (for example, writing isinstance(f, file)).

Post a Comment for "Not Able To Serve Mp3 Files In Django"