sendFileToClient method

Future<bool> sendFileToClient(
  1. String filePath
)

Implementation

Future<bool> sendFileToClient(String filePath) async {
 final file = File(filePath);
 if (await file.exists()) {
   const chunkSize = 64 * 1024; // 64 KB per chunk
   final raf = file.openSync(mode: FileMode.read);
   int totalBytesSent = 0;
   try {
     while (true) {
       final chunk = raf.readSync(chunkSize);
       if (chunk.isEmpty) break; // Exit loop if no more data to read
       _clients.first.add(chunk); // Send chunk to the client
       totalBytesSent += chunk.length; // Track total bytes sent
       debugPrint('Sent chunk of size: ${chunk.length} bytes');
     }
   } finally {
     raf.closeSync(); // Ensure the file is closed after reading
   }
   debugPrint('Total bytes sent: $totalBytesSent');
   debugPrint('File sending completed to client.');
   return true;
 } else {
   debugPrint('File not found: $filePath');
   return false;
 }
  }