未处理的异常:FileSystemException

时间:2020-08-22 15:01:57

标签: flutter file-upload

当我上传.zip文件或.docx时,它无法正常工作,但是当我选择.c文件时,它们可以正常工作 [错误:flutter / lib / ui / ui_dart_state.cc(157)]未处理的异常:FileSystemException:无法使用编码'utf-8'解码数据,路径='/ storage / emulated / 0 / New Text Document.zip'< / p>

  File _file;
  Future upload() async {
    if (_file == null) { return; }
    String path = _file.path.split("/").last;
    var pdf = _file.readAsStringSync();
    var url ="http://192.168.1.112/flutter/upload_file.php";
    var data = { "path": path, "pdf": pdf };
    var response = await http.post(url, body: data);
  }

  Future pickFile() async {
    final myfile = await FilePicker.getFile();
    setState(() {
      _file = File(myfile.path);
    });
  }

这是我尝试使用readAsBytesSync的php文件,它给了我错误

 <?php
    include 'connection.php';
    include 'register.php';
    
    $pdf = $_POST['pdf'];
    
    $file_name = $_POST['path'];

    file_put_contents("uploads\\".$file_name, $pdf);
?>

1 个答案:

答案 0 :(得分:0)

_file.readAsStringSync()

这假设您的文件是纯文本。 *.c文件最有可能是。 zip,doc和pdf文件都不是。

您需要做的是read your file as bytes

var contents = _file.reasAsBytesSync();

现在,我不知道您的API期望得到什么,因此我无法真正帮助您如何传输字节。但这是要走的路。