我有一个Symfony 4 API,我想创建一个可以从angular Application导入csv文件并将其转换为Json以便将其数据插入数据库的操作。 我尝试了这个但没用
/**
* Create many Codes
* @Route("/codes/import", name="code_import", methods={"POST"})
*/
public function importAction(CodeService $codeService, Request $request)
{
$file = $request->files->get('file');
$file->getData();
$csv= file_get_contents($file);
$array = array_map("str_getcsv", explode("\n", $csv));
$json = json_encode($array);
foreach ($json as $value) {
$code =$this->serializer->deserialize(json_encode($value), Code::class, 'json') ;
$codeService->addCode($code);
}
return new Response('', Response::HTTP_CREATED);
}
}