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


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

查看所有标签

猜你喜欢:

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

An Introduction to the Analysis of Algorithms

An Introduction to the Analysis of Algorithms

Robert Sedgewick、Philippe Flajolet / Addison-Wesley Professional / 1995-12-10 / CAD 67.99

This book is a thorough overview of the primary techniques and models used in the mathematical analysis of algorithms. The first half of the book draws upon classical mathematical material from discre......一起来看看 《An Introduction to the Analysis of Algorithms》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

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

UNIX 时间戳转换