📤 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,XFileorpicked.path
import 'dart:io';
var upload = await whatsapp.uploadMediaFile(
file: File('sample_files/sample.png'),
fileType: 'image/png',
);
upload.getMediaId(); //10023589340434
| param | type | required | description |
|---|---|---|---|
file | dynamic | yes | File or List<int> |
fileType | String | yes | File 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
- Native Platforms
- Web Platforms
- Alternative Web 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()}');
//Android, iOS, Windows, macOS, Web, Linux
import 'package:file_picker/file_picker.dart'; // Add this dependency
// Using file picker to get bytes
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.image,
);
if (result != null) {
final selectedFile = result.files.single; // Uint8List
final response = await whatsapp.uploadMediaFile(
file: selectedFile.bytes, // Pass bytes directly
//file: File(result.files.first.path!), //Pass file path using File
fileType: whatsapp.getAutoFileType(filePath: selectedFile.name), // ✅ auto-detect type
);
print('Media ID: ${response.getMediaId()}');
}
// Web or HTML
import 'dart:html' as html;
import 'dart:typed_data';
html.FileUploadInputElement uploadInput = html.FileUploadInputElement();
uploadInput.click();
uploadInput.onChange.listen((e) async {
final files = uploadInput.files;
if (files != null && files.isNotEmpty) {
final file = files[0];
final reader = html.FileReader();
reader.readAsArrayBuffer(file);
reader.onLoadEnd.listen((e) async {
final bytes = reader.result as Uint8List;
final response = await whatsapp.uploadMediaFile(
file: bytes,
fileType: whatsapp.getAutoFileType(filePath: file.name), // ✅ 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
);
}