Created
July 1, 2012 22:25
-
-
Save branflake2267/3029852 to your computer and use it in GitHub Desktop.
FileReader On Load Getting Called More Than Once
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import('dart:html'); | |
void main() { | |
new ImageScaler.start(); | |
} | |
class ImageScaler { | |
ImageScaler.start() { | |
observeFileInput(); | |
} | |
void observeFileInput() { | |
InputElement fileElement = query("#file"); | |
fileElement.on.change.add((Event e) { | |
processFiles(fileElement.files); | |
}, true); | |
} | |
void processFiles(List<File> files) { | |
for (File file in files) { | |
processFile(file); | |
} | |
} | |
void processFile(File file) { | |
print("file.name=${file.name}"); | |
if (!isAnImage(file)) { | |
window.alert("Oops, that wasn't an image, can you try an image?"); | |
return; | |
} | |
readIn(file); | |
} | |
bool isAnImage(File file) { | |
print("file.type=${file.type}"); | |
Pattern pattern = new RegExp(@"(jpeg|png)"); | |
bool b = false; | |
if (file.type.toString().contains(pattern) == true) { | |
b = true; | |
} | |
return b; | |
} | |
void readIn(File file) { | |
FileReader reader = new FileReader(); | |
reader.on.loadEnd.add((e) => scale(reader.result)); | |
reader.readAsDataURL(file); | |
} | |
void scale(String base64) { // getting called twice??? | |
print(base64); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment