最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to read web.File content in Flutter WASM web app using package:webweb.dart? - Stack Overflow

programmeradmin3浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论