当前位置:  开发笔记 > 编程语言 > 正文

使用表单数据Laravel 5.3上载Ajax文件

如何解决《使用表单数据Laravel5.3上载Ajax文件》经验,为你挑选了1个好方法。

我想配置文件上传imageuser服务器,我停留在图像的阿贾克斯上传

我所有的表单数据都将发布到,database包括,image name但文件未上传到服务器

我的看法是

//form
{!! csrf_field() !!}
Image
//ajax $(document).on("click", ".agent-add", function () { var agent_id = $(this).data('id'); $('form').submit(function(event) { event.preventDefault(); $.ajax ({ url: '{{ url('/agents') }}', type: 'POST', data: { "_method": 'POST', "name": $('input[name=name]').val(), "email": $('input[name=email]').val(), "phone_number": $('input[name=phone_number]').val(), "address_city_village": $('input[name=address_city_village]').val(), "address_state": $('input[name=address_state]').val(), "image": $('input[name=image]').val(), "password": $('input[name=password]').val() }, success: function(result) { location.reload(); }, error: function(data) { console.log(data); } }); }); });

我的控制器是

public function store(Request $request)
{
    if (User::where('phone_number', '=', Input::get('phone_number'))->exists()) {
       return $this->respondBadRequest('Phone Number Exists');
    }
    else 
    {
        User::create($request->all());

        return redirect('agents')->with('Success', 'Agent Added');

        if($request->hasFile('image')) {
            $file = $request->file('image');

            //you also need to keep file extension as well
            $name = $file->getClientOriginalName().'.'.$file->getClientOriginalExtension();

            //using array instead of object
            $image['filePath'] = $name;
            $file->move(public_path().'/uploads/', $name);

        }
    }
}

我想我缺少了ajax posting,但是我无法弄清楚

一世 dd($request->all());

结果是

array:9 [?
  "_token" => "heSkwHd8uSIotbqV1TxtAoG95frcRTATgeGL0aPM"
  "name" => "fwe"
  "email" => "sanjiarya2112@gmail.com"
  "phone_number" => "4444422555"
  "address_city_village" => "sgf"
  "address_state" => "gfdgsdf"
  "password" => "ffffff"
  "confirm" => "ffffff"
  "image" => UploadedFile {#208 ?
    -test: false
    -originalName: "Screenshot (8).png"
    -mimeType: "image/png"
    -size: 135920
    -error: 0
    path: "C:\wamp\tmp"
    filename: "php47F2.tmp"
    basename: "php47F2.tmp"
    pathname: "C:\wamp\tmp\php47F2.tmp"
    extension: "tmp"
    realPath: "C:\wamp\tmp\php47F2.tmp"
    aTime: 2017-01-24 06:14:40
    mTime: 2017-01-24 06:14:40
    cTime: 2017-01-24 06:14:40
    inode: 0
    size: 135920
    perms: 0100666
    owner: 0
    group: 0
    type: "file"
    writable: true
    readable: true
    executable: false
    file: true
    dir: false
    link: false
    linkTarget: "C:\wamp\tmp\php47F2.tmp"
  }
]

我在C:\wamp\tmp\php47F2.tmp那儿检查了enen我找不到图像

期待急需的帮助

谢谢



1> Jaymin Panch..:

上载文件时尝试使用FormDatain ajax

试试这个

$('form').submit(function(event) {
    event.preventDefault();
    var formData = new FormData($(this)[0]);
    $.ajax({
        url: '{{ url('/agents') }}',
        type: 'POST',              
        data: formData,
        success: function(result)
        {
            location.reload();
        },
        error: function(data)
        {
            console.log(data);
        }
    });

});

要么

你可以尝试一下 jQuery library

https://github.com/malsup/form

编辑

public function store(Request $request)
{
    if (User::where('phone_number', '=', Input::get('phone_number'))->exists()) {
       return $this->respondBadRequest('Phone Number Exists');
    }
    else 
    {
        $user=User::create($request->all());

        if($request->hasFile('image')) {
           $file = $request->file('image');

           //you also need to keep file extension as well
           $name = $file->getClientOriginalName().'.'.$file->getClientOriginalExtension();

           //using the array instead of object
           $image['filePath'] = $name;
           $file->move(public_path().'/uploads/', $name);
           $user->image= public_path().'/uploads/'. $name;
           $user->save();
        }
        return redirect('agents')->with('Success', 'Agent Added');
    }
}

推荐阅读
TXCWB_523
这个屌丝很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有