Reputation: 35
First time asking a question here... SO,
I have a django web app
where people can upload video files. The video file upload just fine and when they are .mp4 files, they can click on them and immediately play them in the chrome browser. However, if the video file is .mov, it forces the user to download the file to their computer before viewing it. I have tried to capture the file before is saves and change the filename from .mov to .mp4 but its not working.
form = AddAttachmentForm(request.POST, request.FILES)
if form.is_valid():
attachment = form.save(commit=False)
attachment.user = student
attachment.attacher = self.request.user
attachment.date_attached = timezone.now()
attachment.competency = competency
attachment.filename = request.FILES['attachment'].name
if attachment.filename.endswith('.mov'):
attachment.filename.replace('.mov','.mp4')
attachment.save()
Upvotes: 1
Views: 1652
Reputation: 35
This way you are just creating an extension.mov.mp4
use os.path.splitext(), os.rename()
import os
thisFile = "mysequence.mov"
base = os.path.splitext(thisFile)[0]
os.rename(thisFile, base + ".mp4")
Upvotes: 0
Reputation: 3920
Try using this:
import os
form = AddAttachmentForm(request.POST, request.FILES)
if form.is_valid():
attachment = form.save(commit=False)
attachment.user = student
attachment.attacher = self.request.user
attachment.date_attached = timezone.now()
attachment.competency = competency
filename = request.FILES['attachment'].name
ext = os.path.splitext(filename)[1].lower()
if ext == '.mov':
attachment.filename = os.path.splitext(filename)[0] + '.mp4'
else:
attachment.filename = filename
attachment.save()
This should change the file extension as you want, but I don't think changing extension have any effect for browser, because it will check the mime type as well.
Upvotes: 2