Laravel 5 Ajax 文件/图片上传

Laravel 5 Ajax File/Image Upload(Laravel 5 Ajax 文件/图片上传)
本文介绍了Laravel 5 Ajax 文件/图片上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的 Laravel ajax 应用程序有问题,

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

这是我的代码.

阿贾克斯..

/*添加新的分类事件*/$(".addbtn").click(function(){$.ajax({url:'添加目录',数据:{logo:new FormData($("#upload_form")[0]),},数据类型:'json',异步:假,类型:'帖子',过程数据:假,内容类型:假,成功:功能(响应){控制台日志(响应);},});});/*添加新的类别事件*/

刀片模板...

 

<div class="form-group"><label for="catagry_name">标志</label><input type="file" class="form-control" id="catagry_logo"><p class="invalid">输入类别标志.</p>

</表单>

<div class="modelFootr"><button type="button" class="addbtn">添加</button><button type="button" class="cnclbtn">重置</button>

控制器..

公共函数catadd(){if (Input::hasFile('logo')){返回文件存在";}别的{返回文件不存在";}}

路线..

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

我的代码有什么错误???

我无法在 Laravel 控制器中获取文件信息..

我该如何解决这个问题...?

解决方案

有两点需要改变:

从以下位置更改您的 js 文件:

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

致:

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

因为您想发送整个表格.

在您的 html 中:

为您的文件输入字段添加名称

致:

I have an issue in my laravel ajax application,

I cant upload images/files through ajax POST.

here is my code.

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

How can i solve this issue...?

解决方案

Two things to change:

Change your js file from:

 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:

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

这篇关于Laravel 5 Ajax 文件/图片上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)