【问题标题】:upload file in flutter web by file_picker通过file_picker在flutter web中上传文件
【发布时间】:2021-12-15 18:47:25
【问题描述】:

我将file_picker: ^4.2.0 show package 用于我的应用程序。

当我将网页发布为 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
    }
  }

【问题讨论】:

标签: flutter file-upload flutter-web filepicker.io


【解决方案1】:

我使用https://pub.dev/packages/file_picker,但在颤动的网络路径中不支持;

你应该使用字节;

我将文件字节保存在 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;
}

这对我有用:)

【讨论】:

    猜你喜欢
    • 2022-12-21
    • 2021-06-15
    • 2022-07-27
    • 2013-02-26
    • 2010-09-05
    • 2022-11-05
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    相关资源
    最近更新 更多