将flutter Image对象转换为要在服务器上发送的File对象



我是flutter的新手,我有一个签名板返回的图像对象,我想把它发送到接受File的服务器,那么我如何把这个对象发送到服务器。或者我们如何在flutter中将此图像对象转换为File。

Image _sign;
File _uplodfile;
final data = await signatureGlobalKey.currentState.toImage(pixelRatio: 3.0);
final bytes = await data.toByteData(format: ui.ImageByteFormat.png);
if (data != null) {
setState(() {
_sign = Image.memory(bytes.buffer.asUint8List());
});

您可以使用flatter_uploader。

首先将其存储到手机中,然后在发送到服务器之前将其转换为文件。

final data = await signatureGlobalKey.currentState.toImage(pixelRatio: 3.0);
var savedDir = await getApplicationDocumentsDirectory();
String appDocPath = savedDir.path;
await Directory('$appDocPath/Signature/').create(recursive: true);
var file ='$appDocPath/image/${DateUtil().signformattedDate()}.png';
File(file).writeAsBytesSync(bytes.buffer.asInt8List());
submitToServer(file);

submitToServer

void submitToServer(String file) async {
var savedDir = await getApplicationDocumentsDirectory();
String appDocPath = savedDir.path;
var filename = FileUtils.basename(file);
final taskId = await uploader.enqueue(
url: "your upload link",
files: [
FileItem(filename: filename, savedDir: appDocPath, fieldname: "file")
],
method: UploadMethod.POST,
headers: {"apikey": "api_123456", "userkey": "userkey_123456"},
data: {"name": "john"}, // any data you want to send in upload request
showNotification:
false, // send local notification (android only) for upload status
tag: "upload 1"); // unique tag for upload task
}

最新更新