Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload image with Uint8List in FormData sent string instead of binary #485

Closed
1 task done
AlexV525 opened this issue Sep 29, 2019 · 19 comments
Closed
1 task done

Comments

@AlexV525
Copy link
Member

AlexV525 commented Sep 29, 2019

New Issue Checklist

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.
若您有时间,请帮助我解决这个问题,非常感谢!

@wendux
Copy link
Contributor

wendux commented Sep 30, 2019

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

@AlexV525
Copy link
Member Author

AlexV525 commented Sep 30, 2019

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

@wendux
Copy link
Contributor

wendux commented Oct 1, 2019

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

@AlexV525
Copy link
Member Author

AlexV525 commented Oct 1, 2019

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

@AlexV525
Copy link
Member Author

AlexV525 commented Oct 8, 2019

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

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

@AlexV525 AlexV525 changed the title Upload image with Uint8List but backend got string instead of binary Upload image with Uint8List in FormData sent string instead of binary Oct 11, 2019
@YQjiuhao
Copy link

同样的问题

@AlexV525
Copy link
Member Author

AlexV525 commented Oct 21, 2019

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

@YQjiuhao
Copy link

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

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

@UttamPanchasara
Copy link

UttamPanchasara commented Nov 12, 2019

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

@stale
Copy link

stale bot commented Dec 12, 2019

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.

@stale stale bot added the stale label Dec 12, 2019
@AlexV525
Copy link
Member Author

@wendux Please take a look.

@stale stale bot removed the stale label Dec 12, 2019
@stale
Copy link

stale bot commented Jan 11, 2020

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.

@stale stale bot added the stale label Jan 11, 2020
@AlexV525
Copy link
Member Author

Still, it's an issue.

@stale stale bot removed the stale label Jan 12, 2020
@stale
Copy link

stale bot commented Feb 11, 2020

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.

@stale stale bot added stale and removed stale labels Feb 11, 2020
@idootop
Copy link

idootop commented Mar 9, 2020

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 :)

@Purelightme
Copy link

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在文件上传这里确实有问题,试了很多办法都不行

@stale
Copy link

stale bot commented May 1, 2020

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.

@stale stale bot added stale and removed stale labels May 1, 2020
@stale
Copy link

stale bot commented May 31, 2020

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.

@stale stale bot added the stale label May 31, 2020
@AlexV525
Copy link
Member Author

Eww, another library waiting for abandon...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants