第一次在这里提问...所以
我有一个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()
尝试使用这个:
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 类型。
这样你只是在创建一个扩展.mov.mp4
use os.path.splitext((, os.rename((
import os
thisFile = "mysequence.mov"
base = os.path.splitext(thisFile)[0]
os.rename(thisFile, base + ".mp4")