Flutter 颤振:图像base64编码|颤振中base64编码的字节不准确

Flutter 颤振:图像base64编码|颤振中base64编码的字节不准确,flutter,flutter-dependencies,flutter-image,Flutter,Flutter Dependencies,Flutter Image,我试图上传一个图像到服务器上,通过我使用图像裁剪器和选择器库的方式。它工作得很好,但在API调用期间,我需要将其编码为base64,这里的编码字符串与在线image-to-base64编码工具相比是错误的。所以服务器无法保存数据,即使它保存了看起来无效的图像。请帮我解决这个问题 我已经使用Postman(在线base64图像工具)检查了API,它工作得很好 代码: Future<void> uploadProfileImage() async { if (imageFile

我试图上传一个图像到服务器上,通过我使用图像裁剪器和选择器库的方式。它工作得很好,但在API调用期间,我需要将其编码为base64,这里的编码字符串与在线image-to-base64编码工具相比是错误的。所以服务器无法保存数据,即使它保存了看起来无效的图像。请帮我解决这个问题

我已经使用Postman(在线base64图像工具)检查了API,它工作得很好

代码:

Future<void> uploadProfileImage() async {
    if (imageFile == null) return;
    String imageFileName = imageFile.path.split("/").last;
    // String base64ProfileImage = base64.encode(imageFile.readAsBytesSync()); // this gives problem
    

    List<int> imageBytes = imageFile.readAsBytesSync();//
    String encodedFile = base64.encode(imageBytes);//this also gives same problem
log(encodedFile);  
......

Other Stuff
    
  }

我也遇到过同样的问题,我就是这样解决的

Future<String> tobase64(File image) async {
  // I encode files to base64 format
  List<int> imgBytes = await image.readAsBytes();
  String base64img = base64Encode(imgBytes);
  return base64img;
}


  Future getImage(
    ImageSource source,
  ) async {
    final pickedFile = await picker.getImage(source: source);

    if (pickedFile != null) {
      setState(() {
        _image = File(pickedFile.path);
      });

      List<String> _splitted = pickedFile.path.split('.');
// this is the  important line on formating
      _process("data:image/${_splitted.last};base64,");
    }
  }



// this method process and formats your base64 data to the standard format
 Future _process(String format) async {
    baseSix4 = format + await tobase64(_image);

  }
Future-tobase64(文件映像)异步{
//我将文件编码为base64格式
List imgBytes=wait image.readAsBytes();
字符串base64img=base64Encode(imgBytes);
返回base64img;
}
未来getImage(
图像源,
)异步的{
final pickedFile=wait picker.getImage(源代码:source);
if(pickedFile!=null){
设置状态(){
_image=文件(pickedFile.path);
});
List _splitted=pickedFile.path.split('.');
//这是关于格式化的重要一行
_进程(“数据:image/${u splitted.last};base64,”;
}
}
//此方法处理base64数据并将其格式化为标准格式
未来进程(字符串格式)异步{
baseSix4=格式+等待tobase64(_图像);
}

侧面的结果相同,更改之处在于您的代码将“data:image/jpg;base64”文本连接在前面,但其他文本看起来与我之前看到的相同,不仅是前面,您还必须在“data:image/”和“data:image/”之间插入图像数据;base64,'
Future<String> tobase64(File image) async {
  // I encode files to base64 format
  List<int> imgBytes = await image.readAsBytes();
  String base64img = base64Encode(imgBytes);
  return base64img;
}


  Future getImage(
    ImageSource source,
  ) async {
    final pickedFile = await picker.getImage(source: source);

    if (pickedFile != null) {
      setState(() {
        _image = File(pickedFile.path);
      });

      List<String> _splitted = pickedFile.path.split('.');
// this is the  important line on formating
      _process("data:image/${_splitted.last};base64,");
    }
  }



// this method process and formats your base64 data to the standard format
 Future _process(String format) async {
    baseSix4 = format + await tobase64(_image);

  }