文件输入和Dart [英] File input and Dart
问题描述
我正在尝试Dart,但我不知道如何将图像从用户发送到服务器。我有输入标签,可以在DART代码中找到它,但似乎无法读取。我正在尝试类似的东西:
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 read from it. Im trying something like:
InputElement ie = document.query('#myinputelement');
ie.on.change.add((event){<br/>
InputElement iee = document.query('#myinputelement');<br/>
FileList mfl = iee.files;<br/>
File myFile = mlf.item(0);<br/>
FileReader fr = new FileReader();
fr.readAsBinaryString(myFile);
String result = fr.result; //this is always empty
});
使用html包含:
<input type="file" id="myinputelement">
我真的希望您能帮助我,有点卡住了。我可能只是想念如何为FileReader做onload,或者我做错了。
I really hope you cant help me, im kinda stuck. I might just be missing how to do the onload for the filereader, or maybe im doing it totally wrong.
推荐答案
FileReader API是异步的,因此您需要使用事件处理程序。
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);
});
您还需要允许文件从浏览器上传,这可以通过在Chrome中启动来完成带有标志-allow-file-access-from-files
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
这篇关于文件输入和Dart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!