簡體   English   中英

通過file_picker在flutter web中上傳文件

[英]upload file in flutter web by file_picker

我使用file_picker: ^4.2.0顯示我的應用程序

當我將網頁發布為 html 時,會出現一些錯誤。

錯誤: path always null in web release

我獲取文件的代碼:

Future getFile() async {    
    FilePickerResult? result = await FilePicker.platform.pickFiles(
      withReadStream: true,
      type: FileType.custom,
      allowedExtensions: ['png', 'jpeg', 'jpg', 'pdf'],
    );
    if (result != null) {
      PlatformFile file = result.files.single;
      setState(() {
        _file = File(file.path.toString());
        _filePath = file.path;
      });
      _uploadFile();
    } else {
      // file not choose
    }
  }

我使用https://pub.dev/packages/file_picker但在 flutter web 路徑中不支持;

你應該使用字節;

我將文件字節保存在 var _fileBytes 中並在請求中使用;

var request = http.MultipartRequest('POST', Uri.parse('https://.....com'));
request.headers.addAll(headers);
request.files.add(
   http.MultipartFile.fromBytes(
     'image', 
      await ConvertFileToCast(_fileBytes),
      filename: fileName,
      contentType: MediaType('*', '*')
   )
);
request.fields.addAll(fields);
var response = await request.send();

函數 ConvertFileToCast:

ConvertFileToCast(data){
  List<int> list = data.cast();
  return list;
}

它對我有用:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM