In this code, I upload the file to my website via FTP
The file is uploaded properly, but when the file is downloaded, it is corrupted
In the same context, it is possible to send it as an attachment, and when sending the database as an attachment, it arrives in the e-mail completely intact.
With an important note
The size of the damaged file is less than the size of the healthy file by several kilobytes
I tried to compress the file and do the same process and got the same result
The compressed file uploaded to the site is damaged, while the one sent as an attachment arrives intact
Future<void> compressFile(File fileToCompress, String outputPath) async {
List<int> bytes = fileToCompress.readAsBytesSync();
Archive archive = Archive();
ArchiveFile archiveFile = ArchiveFile.noCompress(
outputPath.substring(outputPath.lastIndexOf('/') + 1),
bytes.length,
bytes,
);
archive.addFile(archiveFile);
List<int>? compressedBytes = ZipEncoder().encode(archive);
File(outputPath).writeAsBytesSync(compressedBytes!);
print('File compressed successfully$outputPath');
}
Future<void> uploadDatabaseBackupToFTP() async {
try {
final documentsDirectory = await getApplicationDocumentsDirectory();
final databasePath = join(documentsDirectory.path, 'quicksale.db');
print(databasePath);
// get path of assets directory
// await Share.shareFiles([databasePath],
// text: 'Backup of quicksale database');
await compressFile(
File(databasePath), join(documentsDirectory.path, 'backup.zip'));
final backupPath = join(documentsDirectory.path, 'backup.zip');
final backupFile = File(backupPath);
print(backupFile);
FTPConnect ftpConnect = FTPConnect('ftp.quicksale-eg.com',
user: 'xxx', pass: 'xxx', showLog: true);
await ftpConnect.connect();
print('connected to ftp');
// comppress the file before uploading
await ftpConnect.changeDirectory('/public_html/update');
print('changed directory');
await ftpConnect.makeDirectory('backup');
print('created backup directory');
await ftpConnect.changeDirectory('/public_html/update/backup');
await ftpConnect.uploadFile(
File(backupFile.path),
sRemoteName: '/public_html/update/backup/backup.zip',
);
// sleep for 5 seconds
await ftpConnect.disconnect();
print('disconnected from ftp');
} catch (e) {
print(e);
}
}
Quick Sale is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.