Laravel--ajax--文件上传

本文介绍了一个使用Laravel框架实现文件上传的例子,包括前端HTML表单、样式设置、JavaScript交互逻辑以及后端PHP处理过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Html

 

<form action="{{ URL::route('upload.files')}}" method="post" id="imgForm">
    <input type="file" class="hide-avatar" name="file">
    <input type="text" class="file_import" name="files" value="">
    <button class="btn btn-secondary change-avatar" type="button" ><i class="fa fa-upload"></i>上传</button>
</form>


    

 

Css

 

 /*导入文件上传*/
.hide-avatar{
  display:none;
}
#imgForm{
  display:inline;
}
.file_import{
  display:none;

}


Javascript(使用前引用jquery.js,使用layer.js弹出提示)

 

<script>
    $(function(){

        //ajax 上传
        
$(document).ready(function() {
            //上传图片相关
            
$('.change-avatar').click(function() {
                $('.hide-avatar').click();
            });

            $('#imgForminput[name=file]').on('change',function(){
                layer.load(0, {shade:[0.2,'#616161']});
                $('#imgForm').ajaxForm({
                    url : $(this).prop("action"),
                    post:"POST",
                    beforeSubmit:function(data){
                        $.each(data,function(i){});
                    },
                    headers:{
                        'X-CSRF-TOKEN':$('meta[name="csrf-token"]').attr('content')
                    },
                    success:function(data){
                        console.log(data);
                        layer.closeAll('loading');
                        if(data.status== 0){
                            layer.msg(data.msg,{time:1500},function(){
                                window.location.reload();
                            });
                        }else{
                            layer.msg(data.msg,{time:1500},function(){
                                $(".file_import").val(data.path);
                            });
                        }
                    },
                    dataType:'json'
                }).submit();
            });
        });
    })
</script>


Php

 

路由:

//后台导入上传
Route::post('/upload/files',[
    'as'=> 'upload.files','uses' => 'UploadController@files',
]);

 

 

/*
@ Laravel框架
@ 上传文件
*/
public functionfiles(){
    $file= Input::file('file');
    $clientName= $file-> getClientOriginalName();
    $allowed_extensions= ["xls"];
    if($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(),$allowed_extensions)) {
        $json['status'] = 0;
        $json['msg'] = '请上传xls后缀格式文件';
    }else{
        $destinationPath= 'upload/eximport/'.date('Ymd').'/';
        $extension= $file->getClientOriginalExtension();
        $fileName= date('YmdHis').'.'.$extension;
        $info= $file->move($destinationPath,$fileName);
        if($info){
            $json['status'] = 1;
            $json['msg'] = '上传成功';
            $json['path'] = $destinationPath.$fileName;
        }else{
            $json['status'] = 0;
            $json['msg'] = '上传失败';
        }
    }
    return$json;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值