dart上传文件输入类型文件 [英] dart upload file input type file
本文介绍了dart上传文件输入类型文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Dart和Golang创建一个小型应用程序,并希望将文件上传到服务器.
Using Dart and Golang for a small app and looking to upload a file to server.
找到了类似的内容并将其放在我的.dart文件中:
Found something like this and placed it in my .dart file:
InputElement uploadInput = query('#file'); // my input type file
uploadInput.on.change.add((e) {
// read file content as dataURL
final files = uploadInput.files;
if (files.length == 1) {
final file = files[0];
final reader = new FileReader();
reader.on.load.add((e) {
sendDatas(reader.result);
});
reader.readAsDataURL(file);
}
});
但出现以下错误:
Uncaught TypeError: Cannot read property 'get$on' of null
知道如何处理吗?
我的目标:使用输入类型的文件将文件上传到服务器(文件大小为<1MB)
my goal: using input type file upload file to server (file size, < 1MB)
推荐答案
最后更改了代码,这是最终的工作功能,使用DART上传文件:
Finally changed the code and this is the final working function, uploading file with DART:
updatefileToPlay(Event e, dynamic data, FileUploadInputElement el) {
final files = el.files;
if (files.length == 1) {
final file = files[0];
final reader = new FileReader();
reader.onLoad.listen((e) {
FormData data = new FormData(); // from dart:html
data.append("file", file);
HttpRequest.request('/upload?id='+message.id, method: 'POST', sendData: data).then((HttpRequest r) {
if (r.readyState == HttpRequest.DONE &&
(r.status == 200 || r.status == 0)) {
window.alert("upload complete");
}
});
});
reader.readAsDataUrl(file);
}
}
这篇关于dart上传文件输入类型文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文