php – Laravel 5 Ajax文件/图像上传

栏目: PHP · 发布时间: 7年前

内容简介:http://stackoverflow.com/questions/32367132/laravel-5-ajax-file-image-upload

我在我的laravel ajax应用程序中有一个问题,

我无法通过ajax POST上传图像/文件.

这是我的代码

阿贾克斯..

/*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*/

刀片模板…

<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>

控制器

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

路线..

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

我的代码是什么错误?

我无法获取 laravel 控制器中的文件信息

我如何解决这个问题?

两件事要改变:

更改您的js文件:

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

至:

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

因为你想发送整个表单.

在你的html中:

在文件输入字段中添加名称

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

至:

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

http://stackoverflow.com/questions/32367132/laravel-5-ajax-file-image-upload


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Head First jQuery

Head First jQuery

Ryan Benedetti , Ronan Cranley / O'Reilly Media / 2011-9 / USD 39.99

Want to add more interactivity and polish to your websites? Discover how jQuery can help you build complex scripting functionality in just a few lines of code. With Head First jQuery, you'll quickly g......一起来看看 《Head First jQuery》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换