我正在使用带有 Dropzone.js 的 Laravel 5.8 将文件上传到库,我能够成功地做到这一点。我认为编写一个测试来验证这一点是一种很好的做法,但它总是失败。
我的控制器方法看起来像刚刚被调用store,它看起来像这样:
/**
* Store a new library file in the database
*
* @param StoreArticle $request
* @return void
*/
public function store(StoreLibrary $request)
{
$data = $request->validated();
$category = $data['category'];
$files = $data['file'];
foreach ($files as $file) {
$original_name = $file->getClientOriginalName();
$mime_type = $file->getClientOriginalExtension();
$size = $file->getSize();
// Generate a name for this file
$system_generated_name = sha1(date('YmdHis') . str_random(30)) . '.' . $file->getClientOriginalExtension();
// Store the file on the disk 'library'
$path = Storage::disk('library')->putFileAs(null, $file, $system_generated_name);
// Store a reference to this file in the database
Library::create([
'display_name' => $original_name,
'file_name' => $system_generated_name,
'mime_type' => $mime_type,
'size' => $size,
'disk' => $this->disk,
'storage_location' => $path,
'category' => $category,
]);
}
// Return a JSON response
return response()->json([
'success' => true,
'file' => [
'original_name' => $original_name,
'generated_name' => $system_generated_name,
'path' => $path,
'size' => $size,
]
], 200);
}
小怪兽爱吃肉