第一次在这里提问...
我有一个django web app
,人们可以在其中上传视频文件。视频文件上传正常,当它们是.mp4文件时,他们可以单击它们,然后立即在chrome浏览器中播放它们。但是,如果视频文件是.mov,它将迫使用户在查看文件之前将文件下载到他们的计算机。我尝试过在保存之前捕获文件,并将文件名从.mov更改为.mp4,但无法正常工作。
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()
答案 0 :(得分:0)
尝试使用此:
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()
这应该根据需要更改文件扩展名,但是我认为更改扩展名不会对浏览器产生任何影响,因为它也会检查 mime类型。
答案 1 :(得分:0)
这样您就可以创建一个extension.mov.mp4
使用os.path.splitext(),os.rename()
import os
thisFile = "mysequence.mov"
base = os.path.splitext(thisFile)[0]
os.rename(thisFile, base + ".mp4")