【发布时间】:2021-09-28 09:22:05
【问题描述】:
我想将我的文件上传到 FTP,但我不能使用 $request->file("Fichier1") 因为我是通过 json 从 angular 发送这些数据。
【问题讨论】:
我想将我的文件上传到 FTP,但我不能使用 $request->file("Fichier1") 因为我是通过 json 从 angular 发送这些数据。
【问题讨论】:
正确的方法是发送多部分表单而不是 json。这通常使用 FormData 对象来完成。
const formData = new FormData();
formData.append('file', this.file);
this.httpClient.post(url, formData)
这里有一个教程:https://www.techiediaries.com/angular-formdata/
但是,如果您更愿意使用 json,请从 $request->all() 而不是 $request->file() 获取文件数据。
JSON
{
"name": "Test.pdf",
"type": "application/pdf",
"size": "12100",
"value": "%PDF-1.6↵%����↵4 0 obj↵<</Linearized 1/L 66229/O 6/E 20381/N 1/T 66030/H
[ 836 170]>>↵endobj↵ ↵xref↵4 27↵0000000016 00000 n↵0000001006
00000 n↵0000001066 00000 n↵0000001531 00000 n↵0000001565 00000 n↵0000002420
00000 n↵0000003185 00000 n↵0000003945 00000 n↵0000004352 00000 n↵0000004792
..."
}
控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class MyController extends Controller
{
public function store_file(Request $request) {
$file = $request->all();
Storage::put("directory/".$file['name'], $file);
}
}
如果 json 数据不是请求的根,并且位于名为“file”之类的属性中,请使用 input('file'),而不是 all()
$file = $request->input('file');
【讨论】: