The question was asked before but I have made the things from the other question and I still can not make it work. After clicking button to submit the form's data I get error: Call to a member function guessExtension() on string (500 Internal Server Error). This is took from the response in the network tab. I am sure there is no string, because I have checked and it gives me the data in the form of array with key value pairs(checked it with ordinary request which redirects and dumped the $file variable). This is the code of my controller:
/**
* @Route("/upload", name="uploadpic")
* @param Request $request
* @return JsonResponse|\Symfony\Component\HttpFoundation\Response
*/
public function uploadAction(Request $request){
if(!$request->isXmlHttpRequest()){
return new JsonResponse(
array(
'message' => 'Error',
'status' => 'Not received'
), 400);
}
$user = $this->get('security.token_storage')->getToken()->getUser();
$formed = $this->createForm(UserPicType::class, $user);//dadoh v barzinata User::class i gramna s: class does not implement “Symfony\Component\Form\FormTypeInterface”
$formed->handleRequest($request);
if(isset($request->request)) {
$file = $request->files->get('user_pic_type')['userPic'];
$file = $user->getUserPic();
$fileName = $this->generateUniqueFileName() . '.' . $file->guessExtension();
$file->move(
$this->getParameter('users_directory'),
$fileName
);
$user->setUserPic($fileName);
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
return $this->redirectToRoute('profile');
}
return $this->render('default/index.html.twig',
['formed' => $formed->createView(),
'posts' => null]);
}
private function generateUniqueFileName()
{
return md5(uniqid());
}
Here is my ajax request:
$("#submit_pic").on('click', function(event){
event.preventDefault();
event.stopPropagation();
$.ajax({
type: "post",
url: "/upload",
data: new FormData($("#pic_data")[0]),
processData: false,
contentType: false,
success: function () {
console.log('success');
},
error: function (response) {
console.log(response);
}
});
});
My template:
<form method="POST" enctype="multipart/form-data" action="{{ path('uploadpic') }}">
<p>
<input type="file" id="pic_data" name="user_pic_type[userPic]" required>
</p>
<button type="submit" id="submit_pic" name="" class="btn btn-success btn-sm">
Change
</button>
</form>
When I use dump($file)
:
DefaultController.php on line 62:
UploadedFile {#38 ▼
-test: false
-originalName: "filedd.jpg"
-mimeType: "image/jpeg"
-size: 7256
-error: 0
path: "C:\xampp\tmp"
filename: "phpB394.tmp"
basename: "phpB394.tmp"
pathname: "C:\xampp\tmp\phpB394.tmp"
extension: "tmp"
realPath: "C:\xampp\tmp\phpB394.tmp"
aTime: 2019-01-02 02:08:13
mTime: 2019-01-02 02:08:13
cTime: 2019-01-02 02:08:13
inode: 0
size: 7256
perms: 0100666
owner: 0
group: 0
type: "file"
writable: true
readable: true
executable: false
file: true
dir: false
link: false
linkTarget: "C:\xampp\tmp\phpB394.tmp"
}
So I do get file in the database and my folder when I use ordinary request which refreshes/redirects me, but when I try ajax it spits the error. Any idea why ?