File input and Dart

前端 未结 5 567
余生分开走
余生分开走 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:11

    The FileReader API is asynchronous so you need to use event handlers.

    var input = window.document.querySelector('#upload');
    Element log = query("#log");
    
    input.addEventListener("change", (e) {
      FileList files = input.files;
      Expect.isTrue(files.length > 0);
      File file = files.item(0);
    
      FileReader reader = new FileReader();
      reader.onLoad = (fileEvent) {
        print("file read");
        log.innerHTML = "file content is ${reader.result}";
      };
      reader.onerror = (evt) => print("error ${reader.error.code}");
      reader.readAsText(file);
    });
    

    you also need to allow file uploads from to your browser, which can be done in Chrome by starting it with the flag --allow-file-access-from-files

提交回复
热议问题