i am trying to upload a file from Nuxt3 frontend and Symfony backend. but i am getting this error
Access to fetch at ‘http://localhost:8070/upload-book’ from origin ‘http://localhost:8060’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: It does not have HTTP ok status.
let file = ref<File | null>();
const onSelectFile = (event: any): void => {
file.value = event.target.files[0];
}
const onSaveBook = () => {
try {
if (!file.value) return;
const formData = new FormData();
formData.append('file', file.value);
fetch('http://localhost:8070/upload-book', {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
'Accept': 'application/json',
'Access-Control-Allow-Origin': '*',
},
body: formData,
})
} catch (error) {
console.log(error);
}
#[Route('/upload-book', name: 'app_upload_book', methods: ['POST'])]
public function newBook(Request $request): JsonResponse
{
$file = $request->files->get('file');
if ($file) {
$fileName = uniqid() . '.' . $file->getClientOriginalExtension();
$file->move($this->getParameter('kernel.project_dir') . '/public/uploads', $fileName);
return new JsonResponse('File uploaded successfully', Response::HTTP_OK);
}
return new JsonResponse('No file uploaded', Response::HTTP_BAD_REQUEST);
}