Laravel中Dio为空的Flutter文件上传 [英] Flutter file upload with Dio empty in Laravel

查看:45
本文介绍了Laravel中Dio为空的Flutter文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用 Dio 插件上传文件,也无法确定问题出在哪里.在Laravel中,请求始终为空.

I'm unable to upload files using the Dio plugin and I can not figure out where's the problem. In Laravel the request is always empty.

到目前为止我做了什么:

What have I done so far:

  1. 使用 existsSync()函数仔细检查文件路径是否确实存在
  2. Content-Type 更改为 application/x-www-form-urlencoded
  3. 验证文件是否确实正在上传-似乎可以(?)
  1. Double check if the file path really exists using existsSync() function
  2. Changed the Content-Type to application/x-www-form-urlencoded
  3. Validated if the file is actually being uploaded - seems yes (?)

这是我的颤动代码:

File myFile = new File('/storage/emulated/0/Download/demo.docx');

FormData form = new FormData.from({
  'title': 'Just testing',
  'file': new UploadFileInfo(myFile, 'demo.docx')
});

在通过 POST 发送之前,我检查了文件是否存在并返回true

Before sending through POST i checked if the file exists and returns true

print(myFile.existsSync());

并正确设置 Content-type

Response response = await Dio().post(
  myUrl,
  data: form,
  options: new Options(
    contentType: ContentType.parse("application/x-www-form-urlencoded"),
  ),
);

打印 form 的结果将返回

I/flutter (27929): ----dio-boundary-0118165894
I/flutter (27929): Content-Disposition: form-data; name="title"

I/flutter (27929): ----dio-boundary-1759467036
I/flutter (27929): Content-Disposition: form-data; name="file"; filename="demo.docx"
I/flutter (27929): Content-Type: application/octet-stream

我认为这表示文件正在上传.

Which I believe indicates that the file is being uploaded.

现在在laravel中,每当我输出接收到的内容时,键 file 总是为空,但是键 title 随数据一起提供.

Now in laravel whenever i output the content received it always comes null the key file, but the key title comes with data.

代码 print_r(json_encode($ request-> all()))检索

{"title":"Just testing","file":{}}

print_r(json_encode($ request-> file('file')))也是如此.

我想念什么?

推荐答案

已解决.

这花了我一段时间,但最终我意识到这种方法存在两个问题:

This took me a while to figure it out, but i end up realizing there's two problems with this approach:

  1. Laravel $ request 为空,但 $ _ FILES 不是
  2. 根据文档告诉
  3. ,无法使用数组发送多个文件.
  1. Laravel $request is empty, but $_FILES is not
  2. Sending multiple files can not be sent using arrays as the documentation tells

因此,为了实现我的目标,即允许用户动态选择多个文件并同时上传它们,这是背后的逻辑:

So, in order to achieve my goal which allows the user to select multiple files dynamically and upload them at the same time, here's the logic behind:

必须在不立即设置文件的情况下创建表单:

The form must be created without setting the files right away:

FormData form = new FormData.from(
{
    'title': 'Just testing',
});

由于函数 .from Map< String,因此可以在其后添加动态值.

/* 
 * files = List<String> containing all the file paths
 *
 * It will end up like this:
 *  file_1  => $_FILES
 *  file_2  => $_FILES 
 *  file_3  => $_FILES
 */
for (int i = 0; i < files.length; i++) {
    form.add('file_' + i.toString(),
        new UploadFileInfo(new File(files[i]), files[i].toString()));
}

无需设置其他 Content-Type ,因此就足够了:

There is no need to set up a different Content-Type, therefore this is enough:

Response response = await Dio().post(myUrl, data: form);

Laravel/PHP

忘记通过 $ request-> file()访问 file 的方法,而是使用旧的方法.

Laravel / PHP

Forget about accessing the file through $request->file() and instead use the old school approach.

$totalFiles = count($_FILES);

for ($i = 0; $i < $totalFiles; $i++)
{
    $file = $_FILES['file_' . $i];

    // handle the file normally ...
    $fileName       = basename($file['name']);
    $fileInfo       = pathinfo($file);
    $fileExtension = $fileInfo['extension'];

    move_uploaded_file($file['tmp_name'], $path);
}

这篇关于Laravel中Dio为空的Flutter文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆