Skip to main content
Version: 4.2.0

📤 Upload Media File

Upload a media file to business account

File input in v4.2.0

The file parameter accepts a File, an XFile, a native path (String), a Uint8List or a List<int>.

  • Web platforms: Pass bytes, for example picked.bytes
  • Native platforms: Pass a File, XFile or picked.path
import 'dart:io';

var upload = await whatsapp.uploadMediaFile(
file: File('sample_files/sample.png'),
fileType: 'image/png',
);

upload.getMediaId(); //10023589340434
paramtyperequireddescription
filedynamicyesFile or List<int>
fileTypeStringyesFile Type (ex. image/png)

Helper​

getAutoFileType(filePath: fileUrl) - This methods help you automatically determine the file type.

var file_path = 'sample_files/sample.png'; //or full url
var upload = await whatsapp.uploadMediaFile(
file: File(file_path),
fileType: whatsapp.getAutoFileType(filePath: file_path), // ✅ auto-detect type
);
Cross Platforms Example
//Android, iOS, Windows, macOS, Linux
import 'dart:io';
var file_path = 'sample_files/sample.png';
final response = await whatsapp.uploadMediaFile(
file: File(file_path),
fileType: whatsapp.getAutoFileType(filePath: file_path), // ✅ auto-detect type
);
print('Media ID: ${response.getMediaId()}');
Supported Media Type

Please make sure you are using correct media types. Please find out supported media type here.

Some Tips (Native Platforms)

Here are some tips for media using image_picker Package and file_picker Package

For Image :-

final ImagePicker _picker = ImagePicker();
final XFile? image = await _picker.pickImage(source: ImageSource.gallery);

if (image != null) {
await whatsapp.uploadMediaFile(
file: File(image.path),
fileType: whatsapp.getAutoFileType(filePath: image.name), // ✅ auto-detect type
);
}

For Image, Audio, Video, Document or any file :-

FilePickerResult? result = await FilePicker.platform.pickFiles();

if (result != null) {
PlatformFile file = result.files.first;

await whatsapp.uploadMediaFile(
file: File(file.path!),
fileType: whatsapp.getAutoFileType(filePath: file.name), // ✅ auto-detect type
);
}

Media Return Methods​