1
我想用這個片段如何使用html5與Django的形式?
# 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
在這個片段中
// 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");
}
這樣我就可以使用HTML5,如何將這些片段2合併成1檢查文件的大小?我還發現了一個上傳視頻並檢查大小的java片段,但我找不到有關如何實現它的任何文檔。我不能使用JavaScript因爲我不能相信客戶端
請縮短您的問題,以便它易於閱讀,並且明確地作爲一個具體的事情。刪除所有與引號無關的內容。 – agf 2012-04-17 21:31:17
@agf mikko給我的迴應使我如此憤怒,當我生氣時,我太多地表達我的憤怒,我現在編輯了這個問題 – user 2012-04-17 21:35:08