Make picture from base64 on client-side


How to make a picture from a base64-string to send it to server by using HttpRequest.request?

For example, I have the following base64-string:


Instead of sending it I would like to post a jpeg to server? Is it possible?


Convert Base64 to bytes
How to native convert string -> base64 and base64 -> string

Upload binary as image
Dart how to upload image
(this is the server part, I have to look for the client part)

Client code:

  var request = new HttpRequest()"POST", '')
    ..overrideMimeType("image/your-imagetype") // might be that this doesn't work than use the next line
    ..setRequestHeader("Content-Type", "image/your-imagetype")
    ..onProgress.listen((e) => ...);

    ..onReadyStateChange.listen((e) => ...)
    ..onLoad.listen((e) => ...)

Convert to image:
I think you need to create a dataURL like show here How to upload a file in Dart?
and then use the created dataUrl as src in code like shown here How to load an image in Dart
see also Base64 png data to html5 canvas as @DanFromGermany mentioned in his comment on the question.

It may be necessary to convert List to Uint8List in between.
Please add a comment if you need more information.

Answered By – Günter Zöchbauer

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *