栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Dart / Flutter中将图像发送到api?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Dart / Flutter中将图像发送到api?

最简单的方法是张贴multipart请求就像这篇文章,然后将其上传到服务器。

确保将它们导入文件的开头:

import 'package:path/path.dart';import 'package:async/async.dart';import 'dart:io';import 'package:http/http.dart' as http;import 'dart:convert';

在您的代码中的某处添加此类:

upload(File imageFile) async {          // open a bytestream      var stream = new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));      // get file length      var length = await imageFile.length();      // string to uri      var uri = Uri.parse("http://ip:8082/composer/predict");      // create multipart request      var request = new http.MultipartRequest("POST", uri);      // multipart that takes file      var multipartFile = new http.MultipartFile('file', stream, length,          filename: basename(imageFile.path));      // add file to multipart      request.files.add(multipartFile);      // send      var response = await request.send();      print(response.statusCode);      // listen for response      response.stream.transform(utf8.deprer).listen((value) {        print(value);      });    }

然后使用:

upload(File(filePath));

在您的代码中:

void onTakePictureButtonPressed() {    takePicture().then((String filePath) {      if (mounted) {        setState(() {          imagePath = filePath;          videoController?.dispose();          videoController = null;        });       // initiate file upload       Upload(File(filePath));        if (filePath != null) showInSnackBar('Picture saved to $filePath');      }    });  }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/418468.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号