1
나는 그래서, 어떻게 1로이 두 조각을 결합하는 HTML5를 사용하여 파일의 크기를 확인할 수 있습니다이 조각django 양식과 html5를 사용하는 방법?
// fileInput is a HTMLInputElement: <input type="file" multiple id="myfileinput">
var fileInput = document.getElementById("myfileinput");
// files is a FileList object (simliar to NodeList)
var files = fileInput.files;
for (var i = 0; i < files.length; i++)
{
alert(files[i].name + " has a size of " + files[i].size + " Bytes");
}
로이 조각
# extra.py in yourproject/app/
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, *args, **kwargs):
self.content_types = kwargs.pop("content_types")
self.max_upload_size = kwargs.pop("max_upload_size")
super(ContentTypeRestrictedFileField, self).__init__(*args, **kwargs)
def clean(self, *args, **kwargs):
data = super(ContentTypeRestrictedFileField, self).clean(*args, **kwargs)
file = data.file
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.'))
return data
를 사용하려면? 또한 비디오를 업로드하고 크기를 확인하지만 그것을 구현하는 방법에 대한 모든 문서를 찾을 수없는 자바 스 니펫을 발견. 자바 스크립트를 사용할 수 없습니다. 클라이언트를 신뢰할 수 없습니다.
쉽게 읽을 수 있고 명확한 질문으로 질문을 짧게 작성하십시오. 따옴표가 무엇이든간에 관련이없는 모든 것을 제거하십시오. – agf
@ 미코가 준 반응으로 인해 화가 났고, 화가 났을 때 나는 너무 분노하여 분노를 표출했다. 나는 지금 질문을 편집했다. – user