[英]Multiple file upload not processing in Laravel 6
我正在使用 Laravel 6 编写 API,并且在上传文件时遇到了一些问题,实际上,根本无法上传任何文件。
我正在使用 Postman 来测试 API。 在 params 部分中,我输入了一个名称,然后在 body (form-data) 部分中,我输入了密钥filename
,然后附加了一个文件。
当我发送请求时,它返回all good
状态,但它不会上传文件,也不会将详细信息保存到数据库中。
此外,如果我不附加图像文件,验证器会返回所需的状态,但如果我将其更改为 txt 文件,它什么也不说并返回all good
状态,所以它似乎丢失了filename.*
部分并且只运行第一个filename
验证。
namespace App\Http\Controllers;
use App\ImageUpload;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Validator;
class ImageUploadController extends Controller
{
public function store(Request $request): JsonResponse
{
$validator = Validator::make($request->all(), [
'name' => 'required',
'filename' => 'required',
'filename.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
if ($validator->fails()) {
return response()->json(['error validation failed'], 400);
}
if ($request->hasFile('filename')) {
foreach ($request->file('filename') as $image) {
$fileName = time().'.'.$image->getClientOriginalExtension();
$image->move(public_path() . '/uploads', $fileName);
$imageUpload = new ImageUpload();
$imageUpload->name = $request->name;
$imageUpload->filename = $fileName
$imageUpload->save();
}
}
return response()->json(['All good'], 200);
}
}
如果我dd($resposne)
在最顶部,我尝试上传的文件就在那里,见下文;
+files: Symfony\Component\HttpFoundation\FileBag {#59
#parameters: array:1 [
"filename" => Symfony\Component\HttpFoundation\File\UploadedFile {#44
-test: false
-originalName: "23367298-7883819-image-m-101_1578974555854.jpg"
-mimeType: "image/jpeg"
-error: 0
path: "/tmp"
filename: "phpAJtgNw"
basename: "phpAJtgNw"
pathname: "/tmp/phpAJtgNw"
extension: ""
realPath: "/tmp/phpAJtgNw"
aTime: 2020-01-27 08:59:12
mTime: 2020-01-27 08:59:12
cTime: 2020-01-27 08:59:12
inode: 266560
size: 96981
perms: 0100600
owner: 1000
group: 1000
type: "file"
writable: true
readable: true
executable: false
file: true
dir: false
link: false
}
]
}
$validator = Validator::make($request->all(), [
'name' => 'required',
'filename.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
你的邮递员要求像
filename[] (format type = file) = image1.jpg
filename[] (format type = file) = image2.jpg
filename[] (format type = file) = image3.jpg
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.