이 주제에 관해 많은 질문을 많이 던졌습니다. recently. 동영상을 업로드하기 전에 동영상의 파일 크기를 감지하는 방법은 무엇입니까?
내가 내 업로드 양식models.py
from django.db import models
from app.extra import ContentTypeRestrictedFileField
class upload(models.Model):
""" upload """
name = models.CharField(max_length=100)
description = models.CharField(max_length=250)
file = ContentTypeRestrictedFileField(
upload_to='/media/videos,'
content_types=['video/avi', 'video/mp4', 'video/3gp', 'video/wmp', 'video/flv', 'video/mov'],
max_upload_size=104857600
)
created = models.DateTimeField('created', auto_now_add=True)
modified = models.DateTimeField('modified', auto_now=True)
def __unicode__(self):
return self.name
form.py
from django.db.models import FileField
from django.forms import forms
from django.template.defaultfilters import filesizeformat
from django.utils.translation import ugettext_lazy as _
class ContentTypeRestrictedFileField(FileField):
"""
Same as FileField, but you can specify:
* content_types - list containing allowed content_types. Example: ['application/pdf', 'image/jpeg']
* max_upload_size - a number indicating the maximum file size allowed for upload.
2.5MB - 2621440
5MB - 5242880
10MB - 10485760
20MB - 20971520
50MB - 5242880
100MB 104857600
250MB - 214958080
500MB - 429916160
"""
def __init__(self, content_types=None,max_upload_size=104857600, **kwargs):
self.content_types = kwargs.pop('video/avi', 'video/mp4', 'video/3gp', 'video/wmp', 'video/flv', 'video/mov')
self.max_upload_size = max_upload_size
super(ContentTypeRestrictedFileField, self).__init__(**kwargs)
def clean(self, *args, **kwargs):
data = super(ContentTypeRestrictedFileField, self).clean(*args, **kwargs)
file = data.file
try:
content_type = file.content_type
if content_type in self.content_types:
if file._size > self.max_upload_size:
raise forms.ValidationError(_('Please keep filesize under %s. Current filesize %s') % (filesizeformat(self.max_upload_size), filesizeformat(file._size)))
else:
raise forms.ValidationError(_('Filetype not supported.'))
except AttributeError:
pass
return data
from south.modelsinspector import add_introspection_rules
add_introspection_rules([], ["^app\.extra\.ContentTypeRestrictedFileField"])
을 생성하고이 줄 settings.py
입니다 추가FILE_UPLOAD_MAX_MEMORY_SIZE = 157286400 # 157286400 bytes = 150 MB
가 나는 this snippet을 사용했다, 나는
this snippet을 발견하지만 난 보지 않는 문제가있다되었다하는 djangosnippets 사용자는
If you're ok with letting people use up all your bandwidth for uploading 1GB
files to your servers just to delete them as soon as the upload finishes,
sure it's a great solution.
이 this question를 참조 말한다. 이 질문은 asp.net에 관한 것입니다. 장고를 사용합니다. 장고에서 업로드하기 전에 동영상의 파일 크기를 감지하는 방법
"업로드가 완료 되 자마자 사용자가 모든 대역폭을 사용하여 서버에 1GB 개의 파일을 업로드하여 삭제할 수 있도록 허용한다면 훌륭한 솔루션입니다. " 사용자 1GB의 데이터를 POST 요청을 보낼 수있는 서버는 여전히 데이터를 수신 : '$ 컬 -F [email protected]에 http : // foo.com /'나는 대부분의 호스팅 것을 보라 공급자는 들어오는 대역폭을 청구하지 않습니다. –