Laravel 5 Ajax文件/图片上传

时间:2022-10-18 20:16:36

I have an issue in my laravel ajax application,

我的laravel ajax应用有个问题,

I cant upload images/files through ajax POST.

我不能通过ajax上传图片/文件。

here is my code.

这是我的代码。

Ajax..

Ajax . .

/*Add new catagory Event*/
$(".addbtn").click(function(){
$.ajax({
      url:'add-catagory',
      data:{
        logo:new FormData($("#upload_form")[0]),
        },
      dataType:'json',
      async:false,
      type:'post',
      processData: false,
      contentType: false,
      success:function(response){
        console.log(response);
      },
    });
 });
/*Add new catagory Event*/

Blade template ...

叶片模板…

 <form enctype="multipart/form-data" id="upload_form" role="form" method="POST" action="" >
      <div class="form-group">
        <label for="catagry_name">Name</label>
         <input type="hidden" name="_token" value="{{ csrf_token()}}">
        <input type="text" class="form-control" id="catagry_name" placeholder="Name">
        <p class="invalid">Enter Catagory Name.</p>
      </div>
      <div class="form-group">
        <label for="catagry_name">Logo</label>
        <input type="file" class="form-control" id="catagry_logo">
        <p class="invalid">Enter Catagory Logo.</p>
    </div>

    </form>
    </div>
    <div class="modelFootr">
      <button type="button" class="addbtn">Add</button>
      <button type="button" class="cnclbtn">Reset</button>
    </div>
  </div>

Controller ..

控制器。

public function catadd(){
    if (Input::hasFile('logo'))
    {
       return "file present";
    }
    else{
        return "file not present";
    }
}

Route ..

路线. .

 Route::post('add-catagory',['as'=>'catagory_add','uses'=>'MastersController@catadd']);

What is the error in my code ???

我代码中的错误是什么?

I cant get the file information in laravel controller..

我无法从laravel的控制器上获取文件信息。

How can i solve this issue...?

我怎样才能解决这个问题?

3 个解决方案

#1


21  

Two things to change:

两件事改变:

Change your js file from:

将你的js文件从:

 data:{
    logo:new FormData($("#upload_form")[0]),
 },

To:

:

 data:new FormData($("#upload_form")[0]),

Because you would like to send the whole form.

因为你想把整个表格寄出去。

In your html:

html:

Add a name to your file input field

将名称添加到文件输入字段中

<input type="file" class="form-control" id="catagry_logo">

To:

:

<input type="file" name="logo" class="form-control" id="catagry_logo">

#2


2  

it doesn't work for me because of dataType:'json'. If anyone gets error just remove dataType:'json'.

由于数据类型为“json”,我无法使用它。如果有人出错,就删除dataType:'json'。

#3


1  

Check in your controller what you get when you post:

检查你的控制器当你发布:

echo dd(Input::all()); 

Check files object in php. This in PHP:

在php中检查文件对象。这在PHP中:

$_FILES

Request::file("logo");

Yeah, you're not really posting any data? Is the form really posting?

是的,你并没有发布任何数据?这张表真的贴出来了吗?

#1


21  

Two things to change:

两件事改变:

Change your js file from:

将你的js文件从:

 data:{
    logo:new FormData($("#upload_form")[0]),
 },

To:

:

 data:new FormData($("#upload_form")[0]),

Because you would like to send the whole form.

因为你想把整个表格寄出去。

In your html:

html:

Add a name to your file input field

将名称添加到文件输入字段中

<input type="file" class="form-control" id="catagry_logo">

To:

:

<input type="file" name="logo" class="form-control" id="catagry_logo">

#2


2  

it doesn't work for me because of dataType:'json'. If anyone gets error just remove dataType:'json'.

由于数据类型为“json”,我无法使用它。如果有人出错,就删除dataType:'json'。

#3


1  

Check in your controller what you get when you post:

检查你的控制器当你发布:

echo dd(Input::all()); 

Check files object in php. This in PHP:

在php中检查文件对象。这在PHP中:

$_FILES

Request::file("logo");

Yeah, you're not really posting any data? Is the form really posting?

是的,你并没有发布任何数据?这张表真的贴出来了吗?