我正在尝试使用Dart上传文件,我已经查看过上一个问题,以了解我需要做些什么:How to upload a file in 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是否认不到文件API?
答案 0 :(得分:1)
当变量为null
时,在其上调用的方法抛出NoSuchMethodError
。因此,uploadInput
应该是null
,因为onChange类中存在InputElement。