2

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?

1
  • After reading @NorbertSebők's answer I also added JavaScript tag. Jan 18, 2014 at 9:28

1 Answer 1

2

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");
}
2
  • if i'm using django-storages to save to s3, i'm assuming that web server config will do nothing correct?
    – w--
    Jan 19, 2014 at 0:16
  • @w, the web server will return an error response (413) instead of servicing the request. The Django code will not even notice that there was a failed request. So setting LimitRequestBody is a good idea for enhancing security but a JS check is a more user friendly solution. Jan 19, 2014 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.