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

用FormData上传多张图片问题 #10

Closed
asmh1989 opened this issue May 28, 2018 · 14 comments
Closed

用FormData上传多张图片问题 #10

asmh1989 opened this issue May 28, 2018 · 14 comments

Comments

@asmh1989
Copy link
Contributor

FormData formData = new FormData.from({
   "name": "wendux",
   "age": 25,
   "file1": new UploadFileInfo(new File("./upload.txt"), "upload1.txt")
   "file2": new UploadFileInfo(new File("./upload.txt"), "upload2.txt")
});
response = await dio.post("/info", data: formData)

用改方法, 上传多张的时候, 发现服务端(Go/Gin写的), MultipartForm() 里面只能发现 file1

用postman 测试正常

用postman 测试, key 能用一个, 例如 files, 如果也能操作 后台开发能方便一点

@wendux

@wendux
Copy link
Contributor

wendux commented May 29, 2018

In next version, will support uploading files array .

@keluokeda
Copy link

确实需要乐意上传图片数组的功能

@lucasjinreal
Copy link

@wendux When will next version release?

wendux pushed a commit that referenced this issue Jul 14, 2018
@wendux
Copy link
Contributor

wendux commented Jul 14, 2018

Supported! please update dio to 0.1.3 .

Examples

FormData formData = new FormData.from({
   "name": "wendux",
   "age": 25,
   "file1": new UploadFileInfo(new File("./upload.txt"), "upload1.txt"),
   // Pass multiple files within an Array 
   "files": [
      new UploadFileInfo(new File("./example/upload.txt"), "upload.txt"),
      new UploadFileInfo(new File("./example/upload.txt"), "upload.txt")
    ]
});

:octocat: From gitme Android

@wendux wendux closed this as completed Jul 14, 2018
@jingjianbing000
Copy link

jingjianbing000 commented Jul 20, 2018

HI,更新到 0.1.5 的版本,发现上传多个文件,接收端依旧只能接收到第一个文件,日志如下:

I/flutter (25921): {id: 2739, token: C6114DEE9236F025C92A20831312A697, files: [Instance of 'UploadFileInfo', Instance of 'UploadFileInfo']}
I/flutter (25921): Instance of 'UploadFileInfo'
I/flutter (25921): Instance of 'UploadFileInfo'
I/flutter (25921): 787-318318.jpg [第一个文件]
I/flutter (25921): 1260-387107.jpg [第二个文件]
I/flutter (25921): 583814
I/flutter (25921): [data]={success: 1, files: array (
I/flutter (25921): 0 =>
I/flutter (25921): Phalcon\Http\Request\File::__set_state(array(
I/flutter (25921): '_name' => '787-318318.jpg',
I/flutter (25921): '_tmp' => 'F:\wamp64\tmp\phpC18.tmp',
I/flutter (25921): '_size' => 583420,
I/flutter (25921): '_type' => 'text/plain',
I/flutter (25921): '_realType' => NULL,
I/flutter (25921): '_error' => 0,
I/flutter (25921): '_key' => 'files',
I/flutter (25921): '_extension' => 'jpg',
I/flutter (25921): )),
I/flutter (25921): )}

其中 583814 看起来之包含了第一张图片的尺寸,缺少第二张图片的尺寸

盼回复,谢谢。

@wendux
Copy link
Contributor

wendux commented Jul 20, 2018

@jingjianbing000 你用php原生的全局魔术产量file取一下,可能是你使用的框架不支持(laravel 就不支持),dio多文件上传测试就是用php的。

:octocat: From gitme Android

@asmh1989
Copy link
Contributor Author

0.12的时候试了下也不行,go后端,我还是用官方http的,就是body不支持中文值,要改下

@wendux
Copy link
Contributor

wendux commented Jul 22, 2018

@asmh1989 0.0.12上传文件有问题,然后就紧急修复了,你可以用最新版试试,如果最新版还不行的话, 麻烦贴一下报错信息。

:octocat: From gitme Android

@jingjianbing000
Copy link

@wendux 在php端使用$_FILES接收文件,还是只能接收到1个;

1:flutter 端原始参数如下:
上传文件参数:[File: '/storage/emulated/0/Customize/Wallpapers/387596.jpg', File: '/storage/emulated/0/Customize/Wallpapers/151296.jpg']
其它参数:{id: 2739, token: C6114DEE9236F025C92A20831312A697}

2:flutter dio端加入formdata原始代码如下:
FormData formData = new FormData();

  //加入其它参数
  if (data.length > 0) {
    data.forEach((key, val) {
      formData.add(key, val);
    });
  }

  //加入图片
  if (images.length > 0) {
    var random = new math.Random();
    List<dynamic> img = [];

    for (int a = 0; a < images.length; a++) {
      img.add(new UploadFileInfo(
          images[a],
          random.nextInt(10000).toString() +
              '-' +
              images[a].toString().substring(
                  images[a].toString().lastIndexOf('/') + 1,
                  images[a].toString().length - 1)));
    }

    formData.add("files", img);
  }

  Response response = await dio.post(url, data: formData);
  return response;

3:提交数据时日志如下:
I/flutter (15669): 文件路径:File: '/storage/emulated/0/Customize/Wallpapers/387596.jpg'
I/flutter (15669): 文件名称:973-387596.jpg
I/flutter (15669): FormData:
I/flutter (15669): ----dioBoundary&Happycoding-602485224
I/flutter (15669): Content-Disposition: form-data; name="files"; filename="973-387596.jpg"
I/flutter (15669): Content-Type: text/plain
I/flutter (15669):
I/flutter (15669): 文件大小:408678
I/flutter (15669):
I/flutter (15669): 文件路径:File: '/storage/emulated/0/Customize/Wallpapers/151296.jpg'
I/flutter (15669): 文件名称:4306-151296.jpg
I/flutter (15669): FormData:
I/flutter (15669): ----dioBoundary&Happycoding-602485224
I/flutter (15669): Content-Disposition: form-data; name="files"; filename="4306-151296.jpg"
I/flutter (15669): Content-Type: text/plain
I/flutter (15669):
I/flutter (15669): 文件大小:537897

4:PHP端日志如下:
I/flutter (15669): [data]={success: 1, files: array (
I/flutter (15669): 'files' =>
I/flutter (15669): array (
I/flutter (15669): 'name' => '973-387596.jpg',
I/flutter (15669): 'type' => 'text/plain',
I/flutter (15669): 'tmp_name' => 'F:\wamp64\tmp\php1484.tmp',
I/flutter (15669): 'error' => 0,
I/flutter (15669): 'size' => 537546,
I/flutter (15669): ),
I/flutter (15669): )}

使用0.1.5版本,还是不行;

@wendux
Copy link
Contributor

wendux commented Jul 24, 2018

@jingjianbing000 php下传数组文件时,key要带上"[]", 下面是我在本地测试的代码,是成功的:

  FormData formData = new FormData.from(<String,dynamic>{
    "files[]":[
      new UploadFileInfo(new File("./example/upload.txt"), "upload.txt"),
      new UploadFileInfo(new File("./example/upload.txt"), "uploadc.txt")
    ]
  });

php 端调用var_dump($_FILES)打印的日志:

array(1) {
  ["files"]=>
  array(5) {
    ["name"]=>
    array(2) {
      [0]=>
      string(10) "upload.txt"
      [1]=>
      string(11) "uploadc.txt"
    }
    ["type"]=>
    array(2) {
      [0]=>
      string(10) "text/plain"
      [1]=>
      string(10) "text/plain"
    }
    ["tmp_name"]=>
    array(2) {
      [0]=>
      string(45) "/Applications/XAMPP/xamppfiles/temp/phpK7829F"
      [1]=>
      string(45) "/Applications/XAMPP/xamppfiles/temp/php34xFNW"
    }
    ["error"]=>
    array(2) {
      [0]=>
      int(0)
      [1]=>
      int(0)
    }
    ["size"]=>
    array(2) {
      [0]=>
      int(39)
      [1]=>
      int(40)
    }
  }
}

可见两个文件接收到了。

@asmh1989
Copy link
Contributor Author

@wendux 0.1.6 go 后端还是只能识别 一张, 官方http库没问题

@asmh1989
Copy link
Contributor Author

@wendux 我对比官方http中的代码, 发现

https://github.com/dart-lang/http/blob/0.11.x/lib/src/multipart_request.dart#L110
在每个文件数据后应该加上 \r\n

我这边测试通过

@wendux
Copy link
Contributor

wendux commented Jul 24, 2018

@asmh1989 你是怎么改的?

:octocat: From gitme Android

@asmh1989
Copy link
Contributor Author

已经提交了个pr

AlexV525 added a commit that referenced this issue Feb 13, 2023
Co-authored-by: QuantumGhost <obelisk.reg+git@gmail.com>
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

5 participants