【问题标题】:Convert flutter Image object to File object to send on server将颤振图像对象转换为文件对象以在服务器上发送
【发布时间】:2021-03-15 05:09:34
【问题描述】:

我是 Flutter 的新手,我有一个签名板返回的图像对象,我想将其发送到接受文件的服务器,所以我如何将此对象发送到服务器。或者我们如何在flutter中将此图像对象转换为文件。

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());
  });

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您可以使用flutter_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
      }
    

    【讨论】:

    • 没有为类型“图像”定义吸气剂“缓冲区”。尝试导入定义“缓冲区”的库,将名称更正为现有 getter 的名称,或定义名为“缓冲区”的 getter 或字段。这个错误和
    • 而且我不想在手机中添加多个文件,每次更新签名时只更新一个文件
    • 改用bytes。我已经编辑了我的答案。
    猜你喜欢
    • 1970-01-01
    • 2021-02-05
    • 2020-04-18
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 2018-12-30
    相关资源
    最近更新 更多