Report this

What is the reason for this report?

flutter http MultipartRequest form-data Files post

Posted on February 6, 2021
Moustafa Alsayeh

By Moustafa Alsayeh

Developer

How to upload files or images with http plugin.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

This comment has been deleted

Heya,

Just came across this answer and decided to write some general guidelines for anyone who comes across this in the future despite the old question.

You can use flutter with the http package’s MultipartRequest method to upload files or images. Here’s a basic example:

  1. dart import 'package:http/http.dart' as http; import 'dart:io'; void uploadFile(filePath) async { var request = http.MultipartRequest('POST', Uri.parse(your_url)); request.files.add(await http.MultipartFile.fromPath('fileKey', filePath)); var response = await request.send(); if (response.statusCode == 200) print('Uploaded!'); }

In this example, replace 'fileKey' with the actual file key the server expects, and filePath with the path of the file you want to upload. The your_url should be the endpoint where you want to upload the file.

For more details, you can refer to the official Flutter documentation on Passing Data and HTTP Package.

Hope that this helps!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.