Dio: Upload image with Uint8List in FormData sent string instead of binary

Created on 29 Sep 2019  ·  19Comments  ·  Source: flutterchina/dio

New Issue Checklist

  • [x] I have searched for a similar issue in the project and found one related issue #371 but not worked.

Issue Info

| Info | Value |
| ------------------------------ | ----------------------------------------------------- |
| Platform Name | flutter |
| Platform Version | Not specific |
| Dio Version | 3.0.0 |
| Android Studio / Xcode Version | Android Studio 3.5 |
| Repro rate | all the time (100%) |
| Repro with our demo prj | demo does not including related content |
| Demo project link | can't provided |

Issue Description and Steps

First congratulate with the brand new version of Dio released.
首先恭喜Dio的新版本发布~

In my case, Uint8List image data will be converted to str instead of binary according to PyCharm's log.
在我的业务中,根据PyCharm的日志,Uint8List形式的图片数据被转化成了字符串而不是二进制数据。

As I proceed with the same method, adding a transformer, etc. It just post a string, still.
在我使用了与相关方法后,添加了数据转换等,问题依旧。

Some of my code provided below:
下面提供部分上传实现代码:

static Future updateUserAvatar(Uint8List avatar) async {
    FormData formData = FormData.fromMap({
        "avatar": MultipartFile.fromBytes(avatar),
    });
    return NetUtils.post(
        API.saveUserInfo,
        data: formData,
    );
}

static Future post(String url, {data, headers}) async {
    Dio _dio = Dio();
    return await _dio.post(
        url,
        data: data,
        options: Options(
            headers: headers ?? buildAuthHeader(),
        )
    );
}

Please help me with this when you have spare time, thx a lot.
若您有时间,请帮助我解决这个问题,非常感谢!

stale

Most helpful comment

Having same kind of issue in dio, As in our back-end they are accepting only binary data for file, I would like to have option to pass binary in body instead string,

I'm trying to pass byte[] in body but automatically converting into string and the content-type:application/octet-stream which I'm not sure it's doing anything with that, please help or suggest any solution.


Solution:
What I have done is converting bytes into stream and passing in the body

  List<int> imageBytes = File(path).readAsBytesSync();
  var file = MultipartFile.fromBytes(imageBytes).finalize();

  Dio().post('api/', data: file);

All 19 comments

FormData所有的内容最终都是以2进制上传的,你说“完整log中显示我传出的是string”,这个是根据什么判断的?如果是根据content-type,那么你可以指定一下MultipartFile的contentType

后端的PyCharm显示我传出的内容是str,正常接收应该为binary。
image

  1. 不用重新定义Transformer,把你的transformer删了,transformer在data是Formdata时是没有意义的。
  2. 把你的requestEncoder也删了,干扰,formdata本身就是以二进制流上传的。
  3. Formdata上传的示例example目录中有,请仔细查看。Formdata是经过严格测试的,上传二进制文件是没有问题的,
    另外Formdata上传时content-type是multipart/form-data,#371 并不是说以Formdata方式上传,你这个QueryDict看起来貌似是query参数,你要确认一下后端是以哪种方式接受的。另外和后端调试建议使用抓包工具去排查问题。

恰逢国庆,只能节后再继续调试了,国庆快乐!

1、在formdata example中,并没有使用fromBytes构造FormData的示例,还麻烦进一步补充;
2、在使用fromBytes时,根据业务代码转换出来的Uint8List发送一直被接收为str (PyCharm),为MultipartFile指定contentTypeMediaType("image", "png")无效;
3、最终将数据存至File后构建上传成功。

仍认为其中存在部分问题。

同样的问题

根据群友做的大量测试,目前已定位问题,fromBytes方法指定filename可解决。请考虑将文档及example完善。此原因由何导致?

根据群友做的大量测试,目前已定位问题,fromBytes方法指定filename可解决。请考虑将文档及example完善。此原因由何导致?

我都是指定filename的,反正不行

Having same kind of issue in dio, As in our back-end they are accepting only binary data for file, I would like to have option to pass binary in body instead string,

I'm trying to pass byte[] in body but automatically converting into string and the content-type:application/octet-stream which I'm not sure it's doing anything with that, please help or suggest any solution.


Solution:
What I have done is converting bytes into stream and passing in the body

  List<int> imageBytes = File(path).readAsBytesSync();
  var file = MultipartFile.fromBytes(imageBytes).finalize();

  Dio().post('api/', data: file);

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

@wendux Please take a look.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

Still, it's an issue.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

Having same kind of issue in dio, As in our back-end they are accepting only binary data for file, I would like to have option to pass binary in body instead string,

I'm trying to pass byte[] in body but automatically converting into string and the content-type:application/octet-stream which I'm not sure it's doing anything with that, please help or suggest any solution.

Solution:
What I have done is converting bytes into stream and passing in the body

  List<int> imageBytes = File(path).readAsBytesSync();
  var file = MultipartFile.fromBytes(imageBytes).finalize();

  Dio().post('api/', data: file);

Thanks! It works :)

Having same kind of issue in dio, As in our back-end they are accepting only binary data for file, I would like to have option to pass binary in body instead string,
I'm trying to pass byte[] in body but automatically converting into string and the content-type:application/octet-stream which I'm not sure it's doing anything with that, please help or suggest any solution.
Solution:
What I have done is converting bytes into stream and passing in the body

  List<int> imageBytes = File(path).readAsBytesSync();
  var file = MultipartFile.fromBytes(imageBytes).finalize();

  Dio().post('api/', data: file);

Thanks! It works :)

这样怎么指定上传的文件的key呢?
dio在文件上传这里确实有问题,试了很多办法都不行

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

Eww, another library waiting for abandon...

Was this page helpful?
0 / 5 - 0 ratings