Catch exception when reading a file
authorJoey Schulze <joey@infodrom.org>
Sun, 2 Oct 2011 12:51:51 +0000 (14:51 +0200)
committerJoey Schulze <joey@infodrom.org>
Sun, 2 Oct 2011 12:51:51 +0000 (14:51 +0200)
If the filename is encoded in latin1 getAsBinary cannot read its
content (maybe cannot open it due to different encoding in the browser
and filesystem).

lib/functions.js

index 411f263..b6176e0 100644 (file)
@@ -57,11 +57,23 @@ function ajax_form_submit(func,form,oncomplete)
 
        if (element.nodeName.toLowerCase() == 'input' &&
            element.type.toLowerCase() == 'file') {
+
+           try {
+               var binary = element.files[0].getAsBinary();
+           } catch(err) {
+               error('Problem beim Lesen der Datei');
+               alert("Problem beim Lesen der Datei\n" +
+                     element.files[0].fileName + "\n" +
+                     "Eventuell stimmt das Encoding nicht\n" +
+                     "Die Daten wurden nicht gespeichert");
+               return;
+           }
+
            part = 'Content-Disposition: form-data; ';
            part += 'name="' + fieldName + '"; ';
            part += 'filename="'+ element.files[0].fileName + '"' + CRLF;
            part += "Content-Type: application/octet-stream" + CRLF + CRLF;
-           part += element.files[0].getAsBinary() + CRLF;
+           part += binary + CRLF;
        } else if (element.nodeName.toLowerCase() == 'input' ||
                   element.nodeName.toLowerCase() == 'textarea' ||
                   element.nodeName.toLowerCase() == 'select') {