1

I have one form which contains one file upload.

Form Id is "upload_form"

<input type="file" id="image" name="image"/>

Using javascript onclick function and ajax to pass the image to the controller.

Ajax fn:

$.ajax({
url: 'UploadImage',
data:new FormData($("#upload_form")[0]),
type: "post",
dataType:"JSON",
async:false,

success: function (data) {

console.log(data);

      }
});
}

Routes:

Routes::post('UploadImage','UploadController@Upload');

UploadController:

public function Upload()
{
 $file = Input::file('image');
 $tmpFilePath = '/temp/uploads/';
 $tmpFileName = time() . '-' . $file->getClientOriginalName();
 $path = $tmpFilePath.$tmpFileName;
 $data_file = $file->move(public_path() . $tmpFilePath, $tmpFileName);
 // Error for move() and getClientOriginalName() functions.

}
6
  • so what is the issue ??? Commented Mar 29, 2016 at 10:08
  • I tried to save the image in temporary file and its not working. Commented Mar 29, 2016 at 10:10
  • @Sunil Try this one pastebin.com/P4tGNd09. If it doesn't work, check your write permissions Commented Mar 29, 2016 at 10:19
  • @GONG I am also try do the same but the error coming for 'function move()' and also getClientOriginalName() is also not accepting. Commented Mar 29, 2016 at 10:28
  • 1
    @Sunil try to put enctype="multipart/form-data" in your form Commented Mar 29, 2016 at 10:33

2 Answers 2

2

use this and it should work for you ... :)

 <form action="" method="post" enctype="multipart/form-data">

     {{ csrf_field() }}

    <input type="file" id="image" name="image"/>
    <input type="submit" value="Upload" name="submit">
 </form>
Sign up to request clarification or add additional context in comments.

Comments

1

Thanks @GONG. Changed my form to this. Worked.

<form enctype="multipart/form-data" id="upload_form" role="form" method="POST" action="" >

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.