我正在尝试使用 Dart 上传文件,我查看了上一个问题以了解我需要做什么:如何在 Dart 中上传文件?
但是,当我编译时出现以下错误...
NoSuchMethodError : method not found: 'onChange'
以下是我使用的代码:
import 'dart:html';
void main() {
InputElement uploadInput = query("#myFile");
uploadInput.onChange.listen((e) {
// read file content as dataURL
final files = uploadInput.files;
if (files.length == 1) {
final file = files[0];
final reader = new FileReader();
reader.onLoad.listen((e) {
sendData(reader.result);
});
reader.readAsDataUrl(file);
}
});
}
void sendData(dynamic data) {
final req = new HttpRequest();
req.onreadyStateChange.listen((Event e) {
if (req.readyState == HttpRequest.DONE &&
(req.status == 200 || req.status == 0)) {
window.alert("upload complete");
}
});
req.open("POST", "http://127.0.0.1:8080/upload");
req.send();
}
我错过了导入还是 Chromium 无法识别 File API?