Validating the file field


We want to simply grab the image from the URL without saving it to disk as we are going to perform some validation on the image before writing it.For this reason, we use the is called we have a valid URL value that at least looks like it’s a valid image. We check that the image exists on the server, we make sure it is a valid image by checking it’s mimetype and we make sure it is within our dimension limiations.The user will be presented with a single form field in which they can enter an image URL: Both the URL and the corresponding image (if present) will be validated.It will then be downloaded to our application server and finally it will be assigned to a Django model instance.The following code uses , saving us from fetching a file that exceeds our limitations.We do this by checking the headers for a ’content-length’ key/value which tells us the byte size of the image. It seems like little things make it through with each iteration; but, more would be cool! I would love for them to start exposing more of this stuff.

The full code can be seen over on my Github page and has everything needed to see the example live In this post I’m going to show how we can go about creating a small app that allows the user to upload an image via an URL.

The main steps in this process are as follows: Now we’ll go through some of the various functions we are going to use to validate the image file before and after it is downloaded.

Some of these have been taken from Stackoverflow answers so I’ve added the source where relevant.

But just in case I would add mode="775" and attributes="temporary" to the cffile tag.

In theory that should reset the uploaded file's properties to ensure that it's deletable.

You can find a list of existing mime types on the IANA website.

You must have an account to comment. Please register or login here!