I want to pick a file from computer for upload server, but I don't know how to do?
Asked
Active
Viewed 2,227 times
1 Answers
0
I have written a detailed answer here about accessing the file and reading its contents. It could be a good start for you.
In short
- Create a
FileUploadInputElement
from thedart:html
package like this.FileUploadInputElement element = FileUploadInputElement()..id = "file_input";
. - Use
dart:ui
'splatformViewRegistry.registerViewFactory
method to register your element.
ui.platformViewRegistry.registerViewFactory("add_input", (int viewId) {
return element;
});
- Use
HTMLElementView
widget to attach this element to your web app layout.
HtmlElementView(viewType: 'add_input')
- Use a
FileReader
to read the files available throughFileUploadInputElement.files
as follows.
fileReader.readAsArrayBuffer(element.files[0]);
Handle
onLoad
andonError
events if necessary.On click of your upload button, process the
FileUploadInputElement.files
array to access the files and upload it to the server.

Abhilash Chandran
- 6,803
- 3
- 32
- 50
-
How to associate uploaded files with http requests? – Ven Shine Oct 29 '19 at 03:19
-
you can use `http.post` from the [http](https://pub.dev/packages/http) package. – Abhilash Chandran Oct 29 '19 at 06:06
-
Because the FileUploadInputElement.files object and http's post file are not the same object, I can't put FileUploadInputElement.files to http's post file params. How can I do? – Ven Shine Oct 29 '19 at 06:27
-
You can post the byte array and decode it in the server. check [FileReader](https://api.dartlang.org/stable/2.6.0/dart-html/FileReader-class.html) for more options like reading as text. But format of the data being sent is your choice and is something you will have to research how to do it. – Abhilash Chandran Oct 29 '19 at 06:34
-
Well, I know how to do, Thanks! – Ven Shine Oct 29 '19 at 06:34