【问题标题】:Cannot retrieve length of file, path = 'null' (OS Error: No such file or directory, errno = 2)无法检索文件长度,路径 = \'null\'(操作系统错误:没有这样的文件或目录,errno = 2)
【发布时间】:2022-08-18 02:00:36
【问题描述】:

我是新迁移到颤振最新版本。我一直在将 Flutter v1 迁移到 v3,但是当我使用插件 file_picker: ^5.0.1 画廊和相机工作正常,但每当是文件时,就会出现此错误,我一直在到处搜索,但我没有找到了解决方案,有谁知道我该如何解决这个问题?

 var request = http.MultipartRequest(\'POST\', Uri.parse(baseURL! + urlFiles));
request.fields[\'apiKey\'] = API_KEY;
request.fields[\'username\'] = username as String;
request.fields[\'authenticationCode\'] = authCode as String;
request.files.add(await http.MultipartFile.fromPath(\'myfile\', filename)); //<-- Error

var streamedResponse = await request.send();
final response = await http.Response.fromStream(streamedResponse);

// If the response from the server is correct code == 200 we proceed with the parsing of the json
if (response.statusCode == 200) {
  final responseJson = json.decode(response.body);

这就是我处理文件和图像的方式:

  _pickFile() async {

FilePickerResult? result = await FilePicker.platform.pickFiles();
if (result == null) return;

PlatformFile file = result.files.first;

print(\'File Name: ${file.name}\');
print(\'File Size: ${file.size}\');
print(\'File Extension: ${file.extension}\');
print(\'File Path: ${file.path}\');
//   if (path == null) return;
Navigator.pop(context);
_uploadFile(\'$path\');}

_getPhoto(ImageSource source) async {
    var image = await ImagePicker().getImage(source: source, imageQuality: 50);

    if (image == null) return;

    path = image.path;
    Navigator.pop(context);
    if (kDebugMode) {
      print(\'---> ImagePicker $path\');
    }
    _uploadFile(path as String);}

    标签: android ios flutter filepicker.io


    【解决方案1】:

    不要保存文件的完整路径“仅保存文件夹和文件名”并且每次想要访问它,获取文档目录并将文件夹和文件名作为路径组件附加到它。

    【讨论】:

      【解决方案2】:

      最后,我通过添加这些新行来解决它,一切都按预期工作,并且被服务器接收。

        _pickFile() async {
      final path = await FilePicker.platform.pickFiles(allowMultiple: true);
      String? filePath = path?.files.single.path;
      
      if (path == null) return;
      Navigator.pop(context);
      _uploadFile('$filePath');
      

      }

      【讨论】:

        猜你喜欢
        • 2020-08-10
        • 2020-05-13
        • 2018-03-28
        • 2020-01-13
        • 1970-01-01
        • 2022-01-16
        • 2022-12-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多