#50 Verify that metadata file is readable when selected
Merged 8 years ago by puiterwijk. Opened 8 years ago by rcritten.
rcritten/ipsilon read_metadata  into  master

@@ -38,7 +38,13 @@ 

                      var reader = new FileReader(); // instance of the FileReader

                      reader.readAsDataURL(files[0]); // read the local file

   

-                     reader.onloadend = function(){ // set image data as background of div

+                     reader.onloadend = function(e){ // set image data as background of div

+                         var contents = e.target.result;

+                         if (!contents) {

+                             window.alert('Image file is unreadable')

+                             document.getElementById('uploadFile').value = null;

+                         }

+ 

                          $("#imagePreview").css("background-image", "url("+this.result+")");

                      }

                  }

@@ -69,4 +69,39 @@ 

          <a href="{{ back }}" class="btn btn-default" title="Back">Back</a>

          </form>

      </div>

+ 

+ <script>

+     function verifyFile(filename, objid, failtext) {

+         var reader = new FileReader();

+         reader.readAsDataURL(filename); // read the local file

+ 

+         reader.onloadend = function(e){

+             var contents = e.target.result;

+             if (!contents) {

+                 window.alert(failtext)

+                 document.getElementById(objid).value = null;

+             }

+         }

+     }

+ 

+     $(function() {

+         $("#file").on("change", function()

+         {

+             var files = !!this.files ? this.files : [];

+             if (!files.length || !window.FileReader) return; // no file selected, or no FileReader support

+ 

+             verifyFile(files[0], 'file', 'Metadata file is unreadable');

+         });

+     });

+ 

+     $(function() {

+         $("#image").on("change", function()

+         {

+             var files = !!this.files ? this.files : [];

+             if (!files.length || !window.FileReader) return; // no file selected, or no FileReader support

+ 

+             verifyFile(files[0], 'image', 'Image file is unreadable');

+         });

+     });

+ </script>

  {% endblock %}

no initial comment

+1 great job
The only suggestion I'd have would be to keep the function in the master-admin slide and only use it in other templates if possible.
For example I see a similar function in option_config.html (#upoloadFile) that would use these same checks.

Perhaps it is time we have our own .js file with these things and just apply them as needed, it should be easy enough to convert this code from an anonymous function to something called FileUploader() or similar.

+1 LGTM

I agree with Simo, it might make sense to make this available in a library.

It would be nice but let me point out that the two functions, while similar, actually do quite different things. The uploadFile call slurps in the file for image mime types only and then puts that image as a thumbnail onto the screen.

The new function just reads the file and checks the result to see if there was one and raises an error if not.

So I can see generalizing the current patch for future use without affecting the file uploader one. This is just beyond my meager Javascript skills: being able to register this calback for any file input type by id. Patrick may know of a way.

I'm just happy to have some verification, since generalizing is not obvious I say let's put this in now and worry about generalization later if the need arises.

Thanks for the good work Rob!

I was just thinking you'd want to check if the file is readbale in both cases, not a full merge of both functions.

That was easy enough to do both for the new SP case and when changing the portal image.