File input and Dart

前端 未结 5 591
余生分开走
余生分开走 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条回答
  •  猫巷女王i
    2020-12-11 04:15

    Thanks to the help from this post, I got it to work. I still utilized my event handler in the input tag and made sure that I DID NOT import both dart:io and dart:html, only dart:html is needed.

    This is what my final AppComponent looked like.

    import 'dart:html';
    
    import 'package:angular/angular.dart';
    
    @Component(
      selector: 'my-app',
      styleUrls: ['app_component.css'],
      templateUrl: 'app_component.html',
      directives: [coreDirectives],
    )
    
    class AppComponent {
      // Stores contents of file upon load
      String contents;
    
      AppComponent();
    
      void fileUpload(event) {
        // Get tag and the file 
        InputElement input = window.document.getElementById("fileUpload");
        File file = input.files[0];
    
        // File reader and event handler for end of loading
        FileReader reader = FileReader();
        reader.readAsText(file);
        reader.onLoad.listen((fileEvent) {
          contents = reader.result;
        });
      }
    }
    

    This is what my template looks like:

    File upload test

    Hi! These are the contents of your file:

    {{contents}}

提交回复
热议问题