批量上传图片(jQuery-File-Upload使用)
jQuery-File-Upload
jQuery-File-Upload是一个jquery下的ajax文件上传插件,支持批量上传,github地址:https://github.com/blueimp/jQuery-File-Upload。
官方有个基本的使用教程,如果没有特别需求可以参考这个简单使用
https://github.com/blueimp/jQuery-File-Upload/wiki/Basic-plugin
官方的demo地址,感兴趣可以对照源码分析下,demo分别对应源码basic-plus.html,basic.html等文件
https://blueimp.github.io/jQuery-File-Upload/
批量上传图片
需要引入的css和js文件
<link rel="stylesheet" href="{{asset('js/jquery-fileupload/css/jquery.fileupload.css')}}">
<link rel="stylesheet" href="{{asset('js/jquery-fileupload/css/jquery.fileupload-ui.css')}}">
<script type="text/javascript" src="{{asset('js/jquery-fileupload/js/jquery.ui.widget.js')}}"></script>
<script type="text/javascript" src="{{asset('js/jquery-fileupload/js/jquery.fileupload.js')}}"></script>
<script type="text/javascript" src="{{asset('js/jquery-fileupload/js/jquery.iframe-transport.js')}}"></script>
下载地址 https://github.com/blueimp/jQuery-File-Upload/releases ,下载后解压,将上面需要的文件放入项目相应目录。
html代码
<meta name="csrf-token" content="{{ csrf_token() }}">
<form id="submit_form" action="{{route('work.store')}}" method="post">
{{csrf_field()}}
<table class="add_tab">
<tbody>
<tr>
<th>图片:<a id="start"></a></th>
<td>
<input id="fileupload" type="file" name="files[]" data-url="{{route('batch-upload', ['works'])}}" multiple>
<input id="file_path" type="hidden" name="file_path">
</td>
</tr>
<tr>
<th></th>
<td>
<div>
<div id="progress" style="width:50%;float:left">
<div class="bar" style="width: 0%;height:18px;background:green;"></div>
</div>
<div id="upload_finish" style="display:none;float:left;margin-left:15px;height:18px;line-height:18px;">上传完成</div>
</div>
<div style="clear:both"></div>
<div id = "upload_list">
</div>
</td>
</tr>
<tr>
<th></th>
<td>
<input type="submit" value="提交">
<input type="button" class="back" onclick="history.go(-1)" value="返回">
</td>
</tr>
</tbody>
</table>
</form>
实现的js代码
$(function () {
$('#fileupload').fileupload({
dataType: 'json',
singleFileUploads: false,
beforeSend: function(xhr) {
$('#upload_finish').hide();
$('#progress .bar').css(
'width',
'0%'
);
xhr.setRequestHeader("X-CSRF-TOKEN", $('meta[name="csrf-token"]').attr('content'));
},
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#progress .bar').css(
'width',
progress + '%'
);
if (progress == 100) {
$('#upload_finish').show();
}
},
done: function (e, data) {
var files = data.result.data
for (i in files) {
$('#upload_list').append('<img src="' + files[i]+ '" alt="" style="max-width: 350px; max-height:100px;margin:5px;">');
}
$('#file_path').val(files);
}
});
});
laravel后台处理
Route::post('batch-upload/{dir_name}', 'CommonController@batchUpload')->name('batch-upload');
public function batchUpload(Request $request, $dir_name)
{
try{
$file_path = [];
if($request->hasFile('files')){
$files = $request->file('files');
foreach ($files as $file) {
$entension = $file->getClientOriginalExtension();
$new_name = date('YmdHis').mt_rand(10000,99999).'.'.$entension;
$file_dir = public_path().'/uploads/'.$dir_name;
$file->move($file_dir,$new_name);
$file_path[] = '/uploads/'.$dir_name.'/'.$new_name;
}
}
return $this->success($file_path);;
}catch(\Exception $e){
return $this->error('文件上传失败,请刷新后重试');
}
}
注意:
- 1、ajax调用的时候需要考虑csrf问题,
html中加入头信息
<meta name="csrf-token" content="{{ csrf_token() }}">
beforeSend: function(xhr) {
xhr.setRequestHeader("X-CSRF-TOKEN", $('meta[name="csrf-token"]').attr('content'));
},
- 2、一次上传多个文件,而不是循环调用
singleFileUploads: false,
上面配置是为了一次上传多个文件,否则的话是当你传入多个文件的时候会循环调用你的后台上传api。并且我在跑官方文档的时候发现,一次上传n个图片,会触发n次add回调,并且每次回掉获取data.files对象都是所有的n个文件,想要单独处理每个文件很麻烦,所以我直接每次自动上传,并且都是上传所有选中的文件。
上传完成后效果

批量上传图片(jQuery-File-Upload使用)的更多相关文章
- 用jQuery File Upload做的上传控件demo,支持同页面多个上传按钮
需求 有这么一个需求,一个form有多个文件要上传,但又不是传统的图片批量上传那种,是类似下图这种需求,一开始是用的swfupload做的上传,但是问题是如果有多个按钮的话,就要写很多重复的代码,于为 ...
- jQuery File Upload 单页面多实例的实现
jQuery File Upload 的 GitHub 地址:https://github.com/blueimp/jQuery-File-Upload 插件描述:jQuery File Upload ...
- jQuery File Upload done函数没有返回
最近在使用jQuery File Upload 上传图片时发现一个问题,发现done函数没有callback,经过一番折腾,找到问题原因,是由于dataType: ‘json’造成的,改为autoUp ...
- jQuery File Upload
jQuery File Upload介绍.............................................. 2 实现基本原理......................... ...
- jQuery File Upload 插件 php代码分析
jquery file upload php代码分析首先进入构造方法 __construct() 再进入 initialize()因为我是post方式传的数据 在进入initialize()中的po ...
- 定制jQuery File Upload为微博式单文件上传
日志未经声明,均为AlloVince原创.版权采用『 知识共享署名-非商业性使用 2.5 许可协议』进行许可. jQuery File Upload是一个非常优秀的上传组件,主要使用了XHR作为上传方 ...
- jQuery File Upload文件上传插件简单使用
前言 开发过程中有时候需要用户在前段上传图片信息,我们通常可以使用form标签设置enctype=”multipart/form-data” 属性上传图片,当我们点击submit按钮的时候,图片信息就 ...
- jquery file upload示例
原文链接:http://blog.csdn.net/qq_37936542/article/details/79258158 jquery file upload是一款实用的上传文件插件,项目中刚好用 ...
- jquery file upload 文件上传插件
1. jquery file upload 下载 jquery file upload Demo 地址:https://blueimp.github.io/jQuery-File-Upload/ jq ...
- jQuery File Upload跨域上传
最近在做一个一手粮互联网项目,方案为前后端分离,自己负责前端框架,采用了Requirejs+avalonjs+jquery三个框架完成. 前后端通过跨域实现接口调用,中间也发现了不少问题,尤其是在富文 ...
随机推荐
- Sundy_Android开发深入浅出和高级开发视频教程
Sundy_Android开发深入浅出和高级开发视频教程 放于播音员的网盘中又名:android零基础到高级软件开发工程师培训课程全集(400多讲) 1.课程介绍 2.java重点难点 3.版本控制- ...
- canvas 绘制八卦图
绘制要点: 1.getContext('2d'); -->绘图环境,2维空间 2.fillRect(x,y,w,h); -->矩形:实心(黑色背景) 3.strokeRect(x,y,w, ...
- 如何将位置值写入simotion encoder?
目标: 将变量值(任意实数)写入Encoder,作为encoder的实际位置值.例如,将MP177手轮的值写入编码器,达到SMC30配置手轮的功能. Platform: simotion D435-2 ...
- EF写distinct
在日常开发中常常是这么写的 var logErrorRequest = from l in _logErrorRepository.Table select new { WrongTime = l.W ...
- 2019.03.02 ZJOI2019模拟赛 解题报告
得分: \(10+0+40=50\)(\(T1\),\(T3\)只能写大暴力,\(T2\)压根不会) \(T1\):道路建造 应该是一道比较经典的容斥题,可惜比赛时没有看出来. 由于要求最后删一条边或 ...
- ACM Arabella Collegiate Programming Contest 2015 F. Palindrome 并查集
题目链接:http://codeforces.com/gym/100676/attachments 题意: 给一个字符串,有一些约束条件,两个位置要相同,有一些是问号,求最后有多少种方案回文? 分析: ...
- 字符串处理(POJ1782)
题目链接:http://poj.org/problem?id=1782 解题报告: #include <iostream> #include <cstdio> #include ...
- bootstrap中模态框、模态框的属性
工作中有需要用到模态框的可以看看 <div class="modal fade" id="userModal" tabindex="-1&quo ...
- Linux内存管理 —— 内核态和用户态的内存分配方式
1. 使用buddy系统管理ZONE我的这两篇文章buddy系统和slab分配器已经分析过buddy和slab的原理和源码,因此一些细节不再赘述.所有zone都是通过buddy系统管理的,buddy ...
- ubuntu 报错E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unav E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process us
1.配置xshell,查看虚拟机中ubuntu中网络ip ifconfig 报错 Command 'ifconfig' not found, but can be installed with: su ...