laravel 图片上传 ajax 方式
//后台轮播图上传
$("#img-upload").on('submit',function(e){
e.preventDefault();
var formData = new FormData($(".banner-upload"));
$.ajax({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
type: 'POST',
url: '/admin/banner/create' ,
data: formData ,
processData:false,
//mimeType:"multipart/form-data",
contentType: false,
cache: false,
success:function(data){
console.log(data);
if(data.status){
console.log(data.message);
}
},
error:function(err){
console.log(err);
}
});
form 表单方式
<form class="form-horizontal" action="{{ URL('/admin/banner/create') }}" method="POST" enctype="multipart/form-data" class="banner-upload">
{{ csrf_field() }}
<div class="box-body">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">主题</label>
<div class="col-sm-10">
<input type="text" class="form-control" value="" name="theme" placeholder="设置轮播主题">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">状态</label>
<div class="col-sm-10">
<input type="radio" value="1" name="status">启用
<input type="radio" value="0" name="status">禁用
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">上传图片</label>
<div class="col-sm-10">
<input type="file" name="photo" value="" placeholder="">
<div class="img-wrap">
<img src="" alt="">
</div>
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer clearfix">
<button type="submit" class="btn btn-info pull-right" id="img-upload">提交</button>
</div>
</form>
后台函数
public function BannerCreate(Request $request)
{
$file = $request->file('photo');//获取图片
$theme = $request->theme;//主题
$status = $request->status;//状态
$allowed_extensions = ["png", "jpg", "gif"];
if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
return response()->json([
'status' => false,
'message' => '只能上传 png | jpg | gif格式的图片'
]);
} if ($request->hasFile('photo')) { } $destinationPath = 'storage/uploads/';
$extension = $file->getClientOriginalExtension();
$fileName = str_random(10).'.'.$extension;
$file->move($destinationPath, $fileName);
return Response()->json(
[
'status' => true,
'pic' => asset($destinationPath.$fileName),
'isMake' => $status,
'message' => '新增成功!'
]
);
}
$destinationPath = 'storage/uploads/'; 后面必须要有 / 负责上传后路径访问有问题
<form class="form-horizontal" action="{{ URL('/admin/banner/create') }}" method="POST" enctype="multipart/form-data" class="banner-upload">
{{ csrf_field() }}
<div class="box-body">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">主题</label>
<div class="col-sm-10">
<input type="text" class="form-control" value="" name="theme" placeholder="设置轮播主题">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">状态</label>
<div class="col-sm-10">
<input type="radio" value="1" name="status">启用
<input type="radio" value="0" name="status">禁用
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">上传图片</label>
<div class="col-sm-10">
<input type="file" name="photo" value="" placeholder="">
<div class="img-wrap">
<img src="" alt="">
</div>
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer clearfix">
<button type="submit" class="btn btn-info pull-right" id="img-upload">提交</button>
</div>
</form>
laravel 图片上传 ajax 方式的更多相关文章
- Formdata 图片上传 Ajax
/*图片上传*/ $("点击对象").bind("click", function(e){ $('#form-upload').remove(); $('bod ...
- 用Vue来实现图片上传多种方式
没有业务场景的功能都是耍流氓,那么我们先来模拟一个需要实现的业务场景.假设我们要做一个后台系统添加商品的页面,有一些商品名称.信息等字段,还有需要上传商品轮播图的需求. 我们就以Vue.Element ...
- laravel 图片上传 intervention/image
1. composer require intervention/image 2). 修改 app/config/app.php 添加 ServiceProvider: // 将下面代码添加到 pro ...
- ssm使用Ajax的formData进行异步图片上传返回图片路径,并限制格式和大小
之前整理过SSM的文件上传,这次直接用代码了. 前台页面和js //form表单 <form id= "uploadForm" enctype="multipart ...
- laravel框架图片上传
1.建控制器方法 2.建立路由 绑定控制器方法 3.进行图片上传的配置 修改图片上传的路径 a) config/filesystems.php 修改disks->local->root(图 ...
- Ajax+PHP实现异步图片上传
1.html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <ti ...
- ajax图片上传功能
一.应用场景 当用户需要上传图片当做自己的头像时,预览的时候该图片需要在本地预览,不应该通过网络从服务器上取到之后预览 二.实现方法 1.方法1: 注释:给上传文件的input标签绑定一个change ...
- 用Ajax图片上传、预览、修改图片
首选图片的上传和下载并不是很难,但要注意细节. 一,给出前端图片上传的html代码 1.图片上传的控件 <img src="/${res}/images/default.png&quo ...
- TP框架配合jquery进行3种方式的多图片上传
用的TP5.1框架+jquery 一 使用form表单方式进行多图片上传 html代码: <form action="../admin/admin/cs" enctype=& ...
随机推荐
- 调用PostgreSQL存储过程,找不到函数名的问题
PostgreSQL的表,函数名称都是严格区分大小写的,所以在使用的时候没有注意大小写问题容易导致找不到函数名的错误,但最近两天我们发现,如果函数参数使用了自定义的数据类型,也会发生这个问题. 问题描 ...
- 递归删除资源树 Ztree
前言 最近项目里有这么一个需求:现在有一个用Ztree编写的资源树,当删除资源树的某个节点时,则将此节点下面的所有节点全部删除,这里显然就用到了递归:若此节点被删除后无其它的兄弟节点了,我们还需要将其 ...
- skeleton
响应式模版 伯乐在线:http://hao.jobbole.com/skeleton/ cdn : https://cdn.bootcss.com/skeleton/2.0.4/skeleton.c ...
- ios 让两个tableView同时处于选中状态
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath { [_l ...
- mysql用sql创建表完整实例
create table user_login_latest( id int(11) unsigned NOT NULL AUTO_INCREMENT, user_id int(11) not nul ...
- [移动云计算开发 01] 解决 windows 7 安装设置 nginx 出现端口占用的问题
一开始 到nginx官网 http://nginx.org/en/download.html 下载 1.4.2版本,解压安装到自己希望设置的文件夹即可, 但是打开localhost却出现了 “NOT ...
- eslint常规语法检
"no-alert": 0,//禁止使用alert confirm prompt "no-array-constructor": 2,//禁止使用数组构造器 & ...
- java方法的理解、调用栈与异常处理
一.流程分支 If/else :基于boolean值的双分支 Switch:基于数字(整数.char.byte.枚举).字符串 类型的多分支 Int month =5; Switch 二.方法meth ...
- POJ3660 传递闭包———floyd算法
POJ3660 Cow Contest 题目链接:http://poj.org/problem?id=3660 题意:农名约翰有些奶牛,约翰通过让他们决斗来决定他们的排名,约翰让这些奶牛一对一打完一定 ...
- requests设置Authorization
headers = {"Authorization", "Bearer {}".format(token_string)} r = requests.get(& ...