我只想限制用户可以上传的文件的大小。
我以为maxlength = 20000 = 20k,但这似乎根本不起作用。
我在Rails上运行,而不是在PHP上运行,但我认为在HTML / CSS客户端进行此操作要简单得多,或者作为最后一种使用jQuery的方法。这是如此基本,尽管必须有一些我遗漏或不知道的HTML标记。
希望支持IE7 +,Chrome,FF3.6 +。我想我可以在需要时仅支持IE8 +。
谢谢。
Answers:
这是完全可能的。使用Javascript。
我使用jQuery选择输入元素。我设置了一个on change事件。
$("#aFile_upload").on("change", function (e) {
var count=1;
var files = e.currentTarget.files; // puts all files into an array
// call them as such; files[0].size will get you the file size of the 0th file
for (var x in files) {
var filesize = ((files[x].size/1024)/1024).toFixed(4); // MB
if (files[x].name != "item" && typeof files[x].name != "undefined" && filesize <= 10) {
if (count > 1) {
approvedHTML += ", "+files[x].name;
}
else {
approvedHTML += files[x].name;
}
count++;
}
}
$("#approvedFiles").val(approvedHTML);
});
上面的代码在提交实际发生之前,将我认为值得保留的所有文件名保存到提交页面。我使用jQuery将“批准的”文件添加到输入元素的val中,以便表单提交将发送我要保存的文件的名称。所有文件都将被提交,但是,现在在服务器端,我们必须将它们过滤掉。我还没有为此编写任何代码,但是请发挥您的想象力。我假设可以通过一个for循环并匹配从输入字段发送来的名称并将它们与$ _FILES(PHP Superglobal,抱歉,我不知道ruby file variable)变量相匹配来完成此操作。
我的意思是,您可以在提交文件之前检查文件。我这样做,然后在用户提交表单之前将其输出给用户,以使他们知道他们要上传到我的网站的内容。任何不符合条件的内容都不会显示给用户,因此他们应该知道,不会保存太大的文件。这应该适用于所有浏览器,因为我没有使用FormData对象。
var uploadField = document.getElementById("file");
uploadField.onchange = function() {
if(this.files[0].size > 2097152){
alert("File is too big!");
this.value = "";
};
};
这个例子应该可以正常工作。我将其设置为大约2MB,1MB(以字节为单位)为1,048,576,因此您可以将其乘以所需的限制。
这是jsfiddle示例,更加清晰:https ://jsfiddle.net/7bjfr/808/
您不能在客户端这样做。您必须在服务器上执行此操作。
编辑:这个答案已经过时了!
在进行此编辑时,所有主要浏览器现在都支持HTML文件API 。
我将提供解决方案的更新,但是@ mark.inman.winning已经做到了。
请记住,即使现在可以在客户端上进行验证,但是仍然应该在服务器上进行验证。可以绕过所有客户端验证。
const input = document.getElementById('input')
input.addEventListener('change', (event) => {
const target = event.target
if (target.files && target.files[0]) {
/*Maximum allowed size in bytes
5MB Example
Change first operand(multiplier) for your needs*/
const maxAllowedSize = 5 * 1024 * 1024;
if (target.files[0].size > maxAllowedSize) {
// Here you can ask your users to load correct file
target.value = ''
}
}
})
<input type="file" id="input" />
如果您需要验证文件类型,请在下面的评论中写下,我将分享我的解决方案。
(扰流板:accept属性不是防弹解决方案)
视频文件示例(HTML + Javascript):
function upload_check()
{
var upl = document.getElementById("file_id");
var max = document.getElementById("max_id").value;
if(upl.files[0].size > max)
{
alert("File too big!");
upl.value = "";
}
};
<form action="some_script" method="post" enctype="multipart/form-data">
<input id="max_id" type="hidden" name="MAX_FILE_SIZE" value="250000000" />
<input onchange="upload_check()" id="file_id" type="file" name="file_name" accept="video/*" />
<input type="submit" value="Upload"/>
</form>
const input = document.getElementById('input')
input.addEventListener('change', (event) => {
const target = event.target
if (target.files && target.files[0]) {
/*Maximum allowed size in bytes
5MB Example
Change first operand(multiplier) for your needs*/
const maxAllowedSize = 5 * 1024 * 1024;
if (target.files[0].size > maxAllowedSize) {
// Here you can ask your users to load correct file
target.value = ''
}
}
})
<input type="file" id="input" />
<script type="text/javascript">
$(document).ready(function () {
var uploadField = document.getElementById("file");
uploadField.onchange = function () {
if (this.files[0].size > 300000) {
this.value = "";
swal({
title: 'File is larger than 300 KB !!',
text: 'Please Select a file smaller than 300 KB',
type: 'error',
timer: 4000,
onOpen: () => {
swal.showLoading()
timerInterval = setInterval(() => {
swal.getContent().querySelector('strong')
.textContent = swal.getTimerLeft()
}, 100)
},
onClose: () => {
clearInterval(timerInterval)
}
}).then((result) => {
if (
// Read more about handling dismissals
result.dismiss === swal.DismissReason.timer
) {
console.log('I was closed by the timer')
}
});
};
};
});
</script>