【发布时间】:2019-08-23 23:46:07
【问题描述】:
我无法使用Dio 插件上传文件,我不知道问题出在哪里。在 Laravel 中,请求总是空的。
到目前为止我做了什么:
- 使用
existsSync()函数再次检查文件路径是否真的存在 - 将
Content-Type更改为application/x-www-form-urlencoded - 验证文件是否正在上传 - 似乎是 (?)
这是我的颤振代码:
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
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
我认为这表明文件正在上传。
现在在 laravel 中,每当我输出接收到的内容时,它总是为 null 键 file,但键 title 带有数据。
代码print_r(json_encode($request->all()))检索
{"title":"Just testing","file":{}}
print_r(json_encode($request->file('file'))) 也是如此。
我错过了什么?
【问题讨论】:
-
也许将文件内容解析为 base64 字符串,然后将该 base64 字符串发送到您的 API。
-
@DeesOomens 不能因为尺寸太大。