w--
w--

Reputation: 6667

Django-storage - How to check file size prior to upload?

Storage and Django 1.6:

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?

Upvotes: 2

Views: 1483

Answers (1)

Norbert Sebők
Norbert Sebők

Reputation: 1248

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

Upvotes: 2

Related Questions