gpt4 book ai didi

flutter - 如何解决 FileSystemException : Cannot retrieve length of file, 路径 ='...'

转载 作者:行者123 更新时间:2023-12-05 03:25:36 28 4
gpt4 key购买 nike

    E/flutter ( 8324): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: FileSystemException: Cannot retrieve length of file, path = 'File: '/data/user/0/com.example.upload/cache/file_picker/download (1).jpeg'' (OS Error: No such file or directory, errno = 2)
E/flutter ( 8324): #0 _File.length.<anonymous closure> (dart:io/file_impl.dart:366:9)
E/flutter ( 8324): #1 _rootRunUnary (dart:async/zone.dart:1434:47)
E/flutter ( 8324): #2 _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter ( 8324): <asynchronous suspension>
E/flutter ( 8324): #3 uploadmultipleimage (package:upload/image_upload.dart:42:18)
E/flutter ( 8324): <asynchronous suspension>
E/flutter ( 8324): #4 WriteSQLdataState.build.<anonymous closure> (package:upload/upload_screen.dart:212:33)
E/flutter ( 8324): <asynchronous suspension>
E/flutter ( 8324):



Future uploadImg(List<File> img)async{
final request = http.MultipartRequest("POST", Uri.parse("http://192.168.94.221/easy/uploadfile.php"));
for (final file in img) {
File imageFile = File(img.toString());
var stream =
http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
var length = await imageFile.length();
print(length);
var multipartFile = http.MultipartFile("file",stream, length, filename: basename(file.path));
request.files.add(multipartFile);
print(file.path);
var myRequest = await request.send();
var response = await http.Response.fromStream(myRequest);
if(myRequest.statusCode == 200){
//return jsonDecode(response.body);
print('upload sucess');
}else{
print("Error ${myRequest.statusCode}");
}

}
//request.headers[HttpHeaders.authorizationHeader] = '';
// request.headers[HttpHeaders.acceptHeader] = 'application/json';

final response = await request.send();
if(response.statusCode == 200)
{
print("image sent${response.statusCode}");
}else{
print("ERROR");
}
}

我正在尝试使用 http 将多张图片上传到服务器,我可以一次上传一张图片,但我需要同时上传多个图片文件,这是我的功能:

最佳答案

问题:

问题是您在下面的行中创建了一个新文件,但是您将文件的字符串表示而不是文件路径传递到新文件对象中。

    File imageFile = File(img.toString());

这就是为什么错误消息中的路径是 'File: '/data/user/0/com.example.upload/cache/file_picker/download (1).jpeg' 并且它可以找不到。

解决方案:

有两种方法可以解决这个问题:

  1. 使用文件路径而不是字符串表示创建新的 File 对象。

    改变这一行:

    File imageFile = File(img.toString());

    为此:

    File imageFile = File(file.path);
  2. 直接在循环中使用file 对象而不创建新的File 对象。将您的 for 循环更新为:

    for (final file in img) {
    var stream =
    http.ByteStream(DelegatingStream.typed(file.openRead()));
    var length = await file.length();
    print(length);
    var multipartFile = http.MultipartFile("file",stream, length, filename: basename(file.path));
    request.files.add(multipartFile);
    print(file.path);
    var myRequest = await request.send();
    var response = await http.Response.fromStream(myRequest);
    if(myRequest.statusCode == 200){
    //return jsonDecode(response.body);
    print('upload sucess');
    }else{
    print("Error ${myRequest.statusCode}");
    }

    }

关于flutter - 如何解决 FileSystemException : Cannot retrieve length of file, 路径 ='...',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71960020/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com