File input and Dart

前端 未结 5 590
余生分开走
余生分开走 2020-12-11 03:14

I\'m trying out Dart, but I cant figure out, how to send an image from the user to the server. I have my input-tag, and i can reach this in the DART code, but i cant seem to

5条回答
  •  醉酒成梦
    2020-12-11 04:14

    This is how to read a file using dart:html.

    document.querySelector('#myinputelement`).onChange.listen((changeEvent) {
        List fileInput = document.querySelector('#myinputelement').files;
    
        if (fileInput.length > 1) {
            // More than one file got selected somehow, could be a browser bug.
            // Unless the "multiple" attribute is set on the input element, of course
        }
        else if (fileInput.isEmpty) {
            // This could happen if the browser allows emptying an upload field
        }
    
        FileReader reader = new FileReader();
        reader.onLoad.listen((fileEvent) {
              String fileContent = reader.result;
              // Code doing stuff with fileContent goes here!
        });
    
        reader.onError.listen((itWentWrongEvent) {
              // Handle the error
        });
    
        reader.readAsText(fileInput[0]);
    });
    

提交回复
热议问题