Flutter Image上传问题

时间:2018-08-29 07:38:17

标签: php image api flutter image-uploading

我正在尝试使用以下功能上传图像,但一切正常,只是我想在后期发送图像,而当我尝试使图像什么也没得到时

这是针对通话API

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {
            timer1s();
            handler.postDelayed(this, TimerConstants.ONE_SECOND);
        }
    }, TimerConstants.ONE_SECOND);
    tools.lockScreenOrientation(this);
}

我正在传递到服务器的文件是:

Future getUploadImg(access_token,File _image) async {
  print("Image: $_image");
  String apiUrl = '$_apiUrl/user/upload-profile-image';
  final length = await _image.length();
  final request = new http.MultipartRequest('POST', Uri.parse(apiUrl));
  request.headers['Accesstoken'] = "Bearer $access_token";
  request.files.add(new http.MultipartFile('imagefile',_image.openRead(), length));

  http.Response response = await http.Response.fromStream(await request.send());
  print("Result: ${response.body}");
  return json.decode(response.body);
}

2 个答案:

答案 0 :(得分:0)

我终于得到了结果,如果有人需要帮助,我们需要传递字符串以共享我的工作代码以进行图像共享

fun bind(myCell: MyEntity?) {
    myViewModel.myLiveData.observe(this, Observer {
        // Buala!! Check if it is the cell you want to change and update it.
        if (it != null && myCell != null && it.id == myCell.id) {
           updateCell(it)
        }
    })
}

答案 1 :(得分:0)

为了避免以下问题:

  • 写入失败
  • 连接在...之前关闭
  • Flutter Doctor 错误 - SocketException:写入失败(操作系统错误:管道损坏,errno = 32)

您需要在标题中添加正确的参数。

就我而言,这些问题发生在上传图片和发送 base64 编码请求时。我通过添加以下“连接”标头解决了这个问题:“保持活动”:

  final response = await this.httpClient.put(
    url,
    encoding: Utf8Codec(),
    headers: <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
      'Accept': "*/*",
      'connection': 'keep-alive',
      'Accept-Encoding' : 'gzip, deflate, br',
    },
    body: body,
  );