Flutter Web:文件选择器抛出“无效参数(路径):不得为空”错误

Ram*_*han 8 flutter flutter-web

目标:使用文件资源管理器选择文件并上传到 Firebase 存储。

包:file_picker:^2.1.4

问题:引发错误:“无效参数(路径):不得为空”。

文件资源管理器打开正常,我可以选择一个文件。但是,选择文件后没有任何反应。下面是我迄今为止尝试过的代码:

FilePickerResult result;
File uploadfile;

try{
    result = await FilePicker.platform.pickFiles(type: FileType.custom,
          allowedExtensions: ['jpg', 'pdf', 'doc', 'docx', 'xls', 'xlsx', 'txt'],
        );
} catch(e) { 
    print(e);
}

if(result != null) {
     
    try{
          uploadfile = File(result.files.single.path);
 
          String filename = basename(uploadfile.path);
 
          StorageReference storageRef = FirebaseStorage.instance.ref().child('$path$filename');
 
          final StorageUploadTask uploadTask = storageRef.putFile(uploadfile);
 
          final StorageTaskSnapshot downloadUrl = (await uploadTask.onComplete);
 
          if (downloadUrl.error == null){
 
            final String attchurl = (await downloadUrl.ref.getDownloadURL());
 
          }
    } catch(e) {
       print(e);
    }
Run Code Online (Sandbox Code Playgroud)

我确信代码会在以下位置引发错误:uploadfile = File(result.files.single.path);

我尝试了多个博客中提供的各种建议。即使这里的解决方案没有帮助,我也遇到同样的错误。参见下面的代码:

  FilePickerResult _filePickerResult;
  File uploadfile;
  try {
        _filePickerResult = await FilePicker.platform.pickFiles(
            type: FileType.custom,
           allowedExtensions: ['jpg', 'pdf', 'doc', 'docx', 'xls', 'xlsx', 'txt'],);
      } on PlatformException catch (e) {
        print("Unsupported operation" + e.toString());
      }

      if (_filePickerResult != null) {
        try{
          uploadfile = File(_filePickerResult.files.single.path);

          print(uploadfile);
        }catch(e){
          print(e);
        }
        
  }
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激。谢谢!

*** 更新 ***

当我做:

print(result);
print(result.files);
print(result.files.single);
print(result.files.single.name);
print(result.files.single.size);
print(result.files.single.path);
Run Code Online (Sandbox Code Playgroud)

我得到:

Instance of 'FilePickerResult'
[Instance of 'PlatformFile']
Instance of 'PlatformFile'
FileName01.xlsx
10
null
Run Code Online (Sandbox Code Playgroud)

所以本质上result.files.single.path是失败。希望这可以帮助。谢谢!

Ram*_*han 17

我可能已经解决了这个问题...

显然,根据file_picker wikipath ,在使用网络时总是如此。他们建议改用它来检索文件数据。nullbytes

因此,按照上述说明并进行一些修改,我能够成功上传文件。修改后的代码现在如下所示:

FilePickerResult result;

  try{
    result = await FilePicker.platform.pickFiles(type: FileType.custom,
          allowedExtensions: ['jpg', 'pdf', 'doc', 'docx', 'xls', 'xlsx', 'txt'],
        );
  } catch(e)
  { print(e);
    }

      if(result != null) {
        try{

          Uint8List uploadfile = result.files.single.bytes;
        
          String filename = basename(result.files.single.name);
          
          fs.Reference storageRef = fs.FirebaseStorage.instance.ref().child('$dirpath$filename');
          
          final fs.UploadTask uploadTask = storageRef.putData(uploadfile);
          
          final fs.TaskSnapshot downloadUrl = await uploadTask;
          
          final String attchurl = (await downloadUrl.ref.getDownloadURL());
          
          await AttachmentService(orgid: orgID, orgname: orgName, projid: projID).addattachmentobjs(objType, objID, attchdate, filename, attchurl);
          
      }catch(e) {
          print(e);
        }

      }
Run Code Online (Sandbox Code Playgroud)

基本上,我只是改变了:

FilePickerResult uploadfile = File(result.files.single.path);
Run Code Online (Sandbox Code Playgroud)

到:

Uint8List uploadfile = result.files.single.bytes;
Run Code Online (Sandbox Code Playgroud)

storageRef.putFile(uploadfile);我用的是storageRef.putData(uploadfile);

我确实遇到了MissingPluginException No implementation found for method StorageReference#putDataat final fs.TaskSnapshot downloadUrl = await uploadTask;,我通过将firebase_storage插件更新到最新解决了这个问题。

希望这对将来遇到 Flutter Web 类似问题的人有所帮助。