Files
fforte/lib/screens/sharedMethods/send_file.dart

95 lines
3.0 KiB
Dart

// * Shared method for sending files to the server
// * Allows users to:
// * - Select a file using the system file picker
// * - Send the file contents to the server
// * Legacy widget implementation is kept for reference
import 'package:fforte/screens/sharedMethods/http_request.dart';
import 'package:file_picker/file_picker.dart';
import 'dart:io';
/// Helper class for sending files to the server
class SendFile {
/// Let user pick a file and send its contents to the server
/// Uses the system file picker for file selection
/// Sends file content using the HttpRequestService
static Future<void> sendFile() async {
File? pickedFile;
// Open file picker dialog
FilePickerResult? result = await FilePicker.platform.pickFiles();
if (result != null) {
// Read and send file contents
pickedFile = File(result.files.single.path!);
String fileContent = await pickedFile.readAsString();
await HttpRequestService.httpRequest(saveDataString: fileContent);
}
}
}
// * Legacy widget implementation kept for reference
// * This was a stateful widget version of the file sender
// * with additional UI elements and error handling
/*
class SendFile extends StatefulWidget {
const SendFile({super.key});
@override
State<SendFile> createState() => _SendFileState();
}
class _SendFileState extends State<SendFile> {
File? pickedFile;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Column(
children: [
ElevatedButton(
onPressed: () async {
FilePickerResult? result =
await FilePicker.platform.pickFiles();
if (result != null) {
pickedFile = File(result.files.single.path!);
} else {
pickedFile = File("");
}
},
child: Text(AppLocalizations.of(context)!.pickfile)),
Text(pickedFile.toString()),
ElevatedButton(
onPressed: () async {
final dio = Dio();
final SharedPreferences prefs = await SharedPreferences.getInstance();
String? fileContent = await pickedFile?.readAsString();
dio.options.responseType = ResponseType.plain;
Response response = Response(
requestOptions: RequestOptions(path: ''), statusCode: 400);
try {
response = await dio.post(prefs.getString('apiAddress') ?? "",
data: jsonEncode(fileContent));
} on DioException catch (e) {
if (e.response?.statusCode == 500) {
return;
}
}
if (response.statusCode == 201) {
// Success handling was here
} else {
// Error handling was here
}
},
child: Text(AppLocalizations.of(context)!.sendtoserver))
],
),
);
}
}
*/