1

I want to pick a file from computer for upload server, but I don't know how to do?

Ven Shine
  • 8,739
  • 2
  • 9
  • 24

1 Answers1

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

  1. Create a FileUploadInputElement from the dart:html package like this. FileUploadInputElement element = FileUploadInputElement()..id = "file_input";.
  2. Use dart:ui 's platformViewRegistry.registerViewFactory method to register your element.
ui.platformViewRegistry.registerViewFactory("add_input", (int viewId) {
      return element;
    });
  1. Use HTMLElementView widget to attach this element to your web app layout.

HtmlElementView(viewType: 'add_input')

  1. Use a FileReader to read the files available through FileUploadInputElement.files as follows.

fileReader.readAsArrayBuffer(element.files[0]);

  1. Handle onLoad and onError events if necessary.

  2. 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