Django-storage - 如何在上传之前检查文件大小? [英] Django-storage - How to check file size prior to upload?

查看:304
本文介绍了Django-storage - 如何在上传之前检查文件大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

存储和Django 1.6:

Storage and Django 1.6:

我想限制保存到 s3 的文件大小。这样做的适当方法是什么?
我应该在表单中进行自定义字段验证还是在使用Django-Storage时有更好的方法吗?

I want to restrict filesizes saved to s3. What is the appropriate way to do this? Should I just do custom field validation in the form or is there a better way to do this when using Django-Storage?

推荐答案

验证需要将文件上载到服务器。 建议的方式是设置您的限制网络服务器配置。更好的解决方案是检查JS中的文件大小在上传之前

Validation requires the file to be uploaded to the server. The suggested way is to set the limit in your web server config. A better solution is to check the file size in JS before uploading:

// 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");
}

这篇关于Django-storage - 如何在上传之前检查文件大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆