11

You can use javascript FileReader API to display a preview of an image which is provided from a file input field. This comes in very useful in the sense that you don't have to use server side php and ajax to display the image.

My question though is this:

Is there any limit to the size of the image file being used? Like if a user was to select an image that is 20MB, would the filereader be able to handle it? And would the machines memory potentially become max-ed out?

I'm testing just locally on my machine at the moment. I attempted to load a bmp file (53MB!), which took about 15 seconds to process and display on the page. Other files at 1/2MB generally display instantaneously.

It's probably not required, but here is my HTML file: (FYI: this code works well in supported browsers)

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8"/>
        <title>Dropzone File Upload</title>

    </head>

    <body>

        <img id="uploadPreview" src="default.png" style="width: 100px; height: 100px;" />
        <input id="uploadImage" type="file" name="myPhoto" onchange="PreviewImage();" />
        <p id="uploadProgress">&nbsp;</p>

        <script type="text/javascript">
            function PreviewImage() {
                var avatar_image = document.getElementById("uploadImage");
                var avatar_preview = document.getElementById("uploadPreview");
                var avatar_progress = document.getElementById("uploadProgress");

                if ( window.FileReader ) { //if supports filereader

                    var imgReader = new FileReader();
                    imgReader.readAsDataURL(avatar_image.files[0]); //read from file input

                    imgReader.onloadstart = function(e) {
                        avatar_progress.innerHTML = "Starting to Load";
                    }
                    imgReader.onload = function (imgReaderEvent) {
                        //if file is image
                        if (
                            avatar_image.files[0].type == 'image/jpg' ||
                            avatar_image.files[0].type == 'image/jpeg' ||
                            avatar_image.files[0].type == 'image/png' ||
                            avatar_image.files[0].type == 'image/gif' ||
                            avatar_image.files[0].type == 'image/bmp'
                            ) {
                            avatar_preview.src = imgReaderEvent.target.result;
                        }
                        else {
                            avatar_preview.src = 'filetype.png';
                        }
                    }
                    imgReader.onloadend = function(e) {
                        avatar_progress.innerHTML = "Loaded!";
                    }
                }

                /* For no support, use ActiveX instead */
                else {
                    document.getElementById("uploadPreview").src = "nosupport.png";
                }    

            };
        </script>

    </body>
</html>
2
  • 1
    This answer suggests that memory is your main limitation. Commented Dec 20, 2013 at 15:05
  • Any capacity issue like this always has the same answer: it'll work until it doesn't, and it depends on the capability of the client. A cheap smartphone won't have as much capacity as a desktop computer with lots of RAM. Commented Dec 20, 2013 at 15:05

2 Answers 2

18

It seems in Chrome 45 the limit is 261 MB.

Unfortunately there is no error (FileReader.error == null) when the size is above that limit, the result is just an empty string.

Sign up to request clarification or add additional context in comments.

4 Comments

I just noticed this with some rather large datasets: a 257MB file reads but a 459MB file returns an empty string, Chrome 49.
@PaulAlexander I was unable to find any. I noticed that the number is in the ballpark of the maximum Javascript string length, which is 2^28 or 2^29 or something like that.
This stinks. Any way around this? How am I supposed to serialize file data in an AJAX form?
There really needs to be official documentation for this!
-2

It appears there is no limitation on the filesize. I did the same thing as you in the past and noticed that the time before display is due to the storage in ram/browser. So the delay will depend on the user's computer. If you have to deal with a lot of big images (> 10MB) you can put a gif loader during the loading.

3 Comments

gif loader is a good idea, I must add that in. Normally, I would expect file sizes in the lower range, but I would like to also be able to support cases with larger file sizes, rather than having a file size limit. Would there be any issue with #uploadPreview handling such a large file, or would that be irrelevant?
It will not be an issue for #uploadPreview at all. But remember that if the image has a high resolution (eg.2000x2000) and the #uploadPreview is a small box (eg. 100x100), the image will be downscaled and you will notice some aliasing on the image.
This is incorrect, the correct answer is that there is a hard limit.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.