I'm building a Flutter WASM web application and need to upload files to AWS S3 using presigned URLs. I'm using package:web/web.dart (not dart:html) and struggling to read the content of a web.File object.
Here's my current code:
import 'dart:js_interop';
import 'package:http/http.dart' as http;
import 'package:web/web.dart' as web;
class UploadingStateFile {
final web.File file;
final int orderId;
UploadingStateFile(this.file, this.orderId);
Future<String> uploadFile() async {
String type = file.type;
type = type.isEmpty ? 'application/octet-stream' : type;
final jsonResponse = await S3Calls.getPresignedUrlAttachments(type, orderId, file.name);
final presignedUrl = jsonResponse["presigned_url"];
final url = jsonResponse["url"];
final bytes = await _readFileAsBytes(file); // THIS IS WHAT I NEED HELP WITH
final response = await http.put(
Uri.parse(presignedUrl),
body: bytes,
headers: {'Content-Type': type},
);
if (response.statusCode >= 200 && response.statusCode < 300) return url;
throw Exception('Upload failed: ${response.statusCode} - ${response.body}');
}
Future<Uint8List> _readFileAsBytes(web.File file) async {
// NEED IMPLEMENTATION THAT WORKS WITH package:web/web.dart
}
}
Thank you