繁体   English   中英

Laravel 5.3文件上载调用字符串上的成员函数getClientOriginalName()

[英]Laravel 5.3 File Upload Call to a member function getClientOriginalName() on string

我正在尝试使用Ajax在laravel 5.3中上传图像。

我的刀片代码:

 <form method="post" class="inline" enctype="multipart/form-data" >
<input style="margin-top:0px;" type="file" name="file" id="file" class="btn btn-block btn-primary"/>
<button type="button" class="btn btn-primary" data-toggle="modal" onclick="create1()">Create</button>
</form>

我的ajax电话:

 function create1()
{
    var photo = $("#file").val();

    $.ajax({
            url: '<?= URL:: to('store') ?>',
            type: 'GET',
            async : false,
        data:
            {

                'photo':photo,

            },
            success: function(e)
            {
                if(e == 0)  
                {
                     alert("Success Full Created"); 

                }
                else
                {   
                     alert("Error");
                }
            }   


    });


}

路由呼叫:

Route::get('store','admin\ProductController@store');

控制器调用:

public function store(Request $request)
{
    $post = $request->all();

    $imageName =  $file->getClientOriginalName();

    $imagemove= $file->move(public_path('images'),$imageName);

    $data123 = array (   "photo"=> $imagemove,  );

    $check222 = DB::table('product') -> insert($data123);   

  }

使用当前的解决方案很难调试,因为您使用的是AJAX请求来上传文件。

您无法通过GET上传文件!

因此,请执行以下操作:

  • 禁用AJAX请求
  • 确保您的提交按钮的type="submit"
  • 将您的路线更改为Route::post('store','admin\\ProductController@store');
  • 测试上传

您的html表单应为:

<form method="post" class="inline" enctype="multipart/form-data" >
    <input style="margin-top:0px;" type="file" name="file" id="file" class="btn btn-block btn-primary"/>
    <input type="submit" class="btn btn-primary" value="Create">
</form>

您的route.php或路由文件:

Route::post('store','admin\ProductController@store');

好 ... ?

如果确定上传成功,请插入您的AJAX上传请求。

一步一步,我会跟着你

<form method="post" class="inline" enctype="multipart/form-data" >
<input style="margin-top:0px;" type="file" name="file" id="file" class="btn btn-block btn-primary"/>
<button type="button" class="btn btn-primary" data-toggle="modal" onclick="create1()">Create</button>

阿贾克斯电话

function create1(){

var data = new FormData();
data.append('file', $('#file').get(0).files[0]);

$.ajax({
        type:'post',
        url:<?= URL:: to('store') ?>,
        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
        dataType:'HTML',
        contentType: false,
        processData: false,
        data:data,
        success:function(data){
            if(e == 0)  
            {
                 alert("Success Full Created"); 

            }
            else
            {   
                 alert("Error");
            }
        }
    });}

控制器:

public function store(Request $request){
    $post = $request->all();

    $imageName =  $file->getClientOriginalName();

    $imagemove= $file->move(public_path('images'),$imageName);

    $data123 = array (   "photo"=> $imagemove,  );

    $check222 = DB::table('product') -> insert($data123);   
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM