0

This is my jQuery code:

$.post('../myprofile/storeitem',
{
name:           $("#name").val(), 
price:          $("#price").val(),
description:    $("#description").val(),
city:           $("#city").val(),
telephone:      $("#telephone").val(),
address:        $("#address").val(),
picture:        $("#picture").val()
},

In my controller I have:

$validation = Validator::make(Input::all(), Product::$rules);

 if($validation->fails()) {
     return Response::json(['success' => false, 'error' => $validation->errors()->toArray()]);  
 }
 else { 
     $image = Input::file('picture');
     $filename = $image->getClientOriginalName();

My rule for validating the picture field which is located in the Products model among other rules:

'picture' => 'required|image|mimes:jpg,jpeg,bmp,png|max:2000'

After a partial successful validation i get the error:

{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Call to a member function getClientOriginalExtension() on a non-object","file":"C:\\wamp\\www\\pro\\app\\controllers\\MyprofileController.php","line":125}}
Michał
  • 2,456
  • 4
  • 26
  • 33
Greeneerg
  • 11
  • 2

1 Answers1

0

As I understand, you are trying to send BLOB over AJAX. If I'm correct, this neat, nice code with $.post is far beyond the reality.

First of all, you should understand that reading files is not a straightforward solution.

In HTML5 you can use certain objects to read the file contents and send it over AJAX. You might refer to the following question for more details.

Further, for browsers that don't support these objects, you have to use some plug-ins, like this or this. I used first, but at the time of my experience, it suffered from several issues. But you need to check.

Finally, there are browsers that don't support both, HTML5 and flash (older mobile devices) you have to target them separately. You might use IFrame method, but it requires you some hard-core coding. The SO question jQuery iframe file upload can give you some basic start.

As of your current state of affairs, you are getting and sending a string value, not the actual file value. It is discussed a little bit here: Can't get value of input type="file"?

The error is occurring because $image = Input::file('picture'); is returning something other than the expected image object. And it doesn't have method getClientOriginalExtension

Community
  • 1
  • 1
Oybek
  • 7,016
  • 5
  • 29
  • 49