I want to upload a video file to my cloud storage but I want to check the size of the video file before I upload. how to achieve this
Asked
Active
Viewed 4.9k times
48
-
Please mark this as accepted answer: https://stackoverflow.com/a/66473018/8164116 – Daksh Gargas Oct 11 '21 at 06:55
5 Answers
101
If you have the path of the file, you can use dart:io
var file = File('the_path_to_the_video.mp4');
You an either use:
print(file.lengthSync());
or
print (await file.length());
Note: The size returned is in bytes
.

CopsOnRoad
- 237,138
- 77
- 654
- 440

Günter Zöchbauer
- 623,577
- 216
- 2,003
- 1,567
-
1when I tried I got this: hashcode:844733706 Type (FileSystemException) "Cannot retrieve length of file" path:"File: '/storage/emulated/0/WhatsApp/Media/WhatsApp Video/VID-20181028-WA0018.mp4'" – Norbert Oct 29 '18 at 12:07
-
Can you please try `file.resolveSymbolicLinksSync().lengthSync()` – Günter Zöchbauer Oct 29 '18 at 12:18
-
-
Ok, then `File(file.resolveSymbolicLinksSync()).lengthSync()` – Günter Zöchbauer Oct 29 '18 at 12:40
-
1
-
1
-
5https://gist.github.com/zzpmaster/ec51afdbbfa5b2bf6ced13374ff891d9 combine it with this. Perfect ! – Asad S Dec 25 '20 at 15:16
-
2If you are using the latest ImagePicker Which Returns PickedFile instead of File. ```formatBytes(await File(image.path).lengthSync(), 2) ``` – Mahesh Jamdade Mar 10 '21 at 10:39
50
// defined the function
getFileSize(String filepath, int decimals) async {
var file = File(filepath);
int bytes = await file.length();
if (bytes <= 0) return "0 B";
const suffixes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
var i = (log(bytes) / log(1024)).floor();
return ((bytes / pow(1024, i)).toStringAsFixed(decimals)) + ' ' + suffixes[i];
}
// how to call the function
print(getFileSize('file-path-will-be-here', 1));
// output will be as string like:
97.5 KB
This worked for me. I hope, this will also help you. Thanks a lot for asking question.

Kamlesh
- 5,233
- 39
- 50
-
Thank you Kamlesh. This is almost matching with the real size of the file. In the console is saying 2.72 MB and in filesystem is showing 2.9 MB. – Florentin Lupascu Mar 05 '21 at 18:00
-
@FlorentinLupascu I am not sure but this may be different due to 1 MB = 1000 KB instead of 1024 KB. – Kamlesh Mar 06 '21 at 08:44
-
-
-
@LearnToday dear, I did not check it with a folder yet, checked only for file only. Thanks. – Kamlesh Sep 10 '21 at 08:43
-
-
@OttomanCoder `decimals` param is used to set number of precision after decimal. eg. If you want to get 2 precision after integer decimal value in result then you can just pass param `2` in `getFileSize('file-path-will-be-here', 2);` function which will result you `97.53 KB`. I hope, now you are clear about `int decimals`. Thanks for sharing your query :) – Kamlesh Jan 15 '22 at 15:52
-
Just to point it out, since it is `1024` here, they should be `KiB` `MiB` etc – http8086 Jun 20 '22 at 19:29
-
17
This is work for me.
final file = File('pickedVid.mp4');
int sizeInBytes = file.lengthSync();
double sizeInMb = sizeInBytes / (1024 * 1024);
if (sizeInMb > 10){
// This file is Longer the
}

sushant singh
- 696
- 1
- 8
- 18
5
If you are using the latest ImagePicker with xFile type then,
final XFile? photo = await _picker.pickImage(source: ImageSource.camera);
print(File(photo.path).lengthSync());
will return the file size in bytes.

Zeeshan Ansari
- 1,413
- 12
- 21
1
import 'dart:io';
//Here's the method to get file size:
double getFileSize(File file){
int sizeInBytes = file.lengthSync();
double sizeInMb = sizeInBytes / (1024 * 1024);
return sizeInMb;
}
//Alternatively for extension:
extension FileUtils on File {
get size {
int sizeInBytes = this.lengthSync();
double sizeInMb = sizeInBytes / (1024 * 1024);
return sizeInMb;
}
}

Mubashar Hussain
- 156
- 1
- 9