网上jquery-file-upload的例子 都过于简单,在项目中这个插件经常使用,写个例子供参考。

下面介绍 用插件实现图片异步上传的代码。

1   比要的js一个都不能少,他们之间是有依赖关系的。

jquery-1.8.2.min.js 
jquery.ui.core.js 
jquery.ui.widget.js 
jquery.fileupload.js 
jquery.iframe-transport.js 
jquery.fileupload-process.js 
jquery.fileupload-validate.js 
(最后2个js是有依赖的,缺少的话acceptFileTypes,maxFileSize 不会进行验证)

2  贴代码:

<script>
$(function () { uploadImageAjaxDelete = function (url,obj){
$.ajax({url:url,async:false,dataType:"text",success:function(data){
if(data=='1'){
//如果删除成功,恢复file的使用,同时是图片渐变消失
$(obj).parent().children("input[type='file']").removeAttr("disabled");
$(obj).parent().children("img").fadeOut("slow",function(){
$(this).add($(obj).parent().children("a")).add($(obj).parent().children("input:hidden")).add($(obj).parent().children("br")).remove();
});
}else{
alert('图片删除失败!');
}
}});
} $("input[type='file']").fileupload({
url: 'image_ajax_save.action',
dataType: 'json',
autoUpload: true,
acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
maxFileSize: 2097152// 2 MB
}).on('fileuploadadd', function (e, data) {
$(this).parent().children("label").remove();
$("<p class='uploadImgLoad'>上传中... 0%</p>").appendTo($(this).parent());
$(this).attr("disabled",true);
}).on('fileuploadprocessalways', function (e, data) {
if(data.files.error){
$(this).parent().children("p").remove();
$(this).removeAttr("disabled");
if(data.files[0].error=='acceptFileTypes'){
$(this).parent().append("<label class='error'>图片类型错误</label>");
}
if(data.files[0].error=='maxFileSize'){
$(this).parent().append("<label class='error'>图片不能大于2M</label>");
}
}
}).on('fileuploadprogressall', function (e, data) {
var $p = $(this).parent().children("p");
var progress = parseInt(data.loaded / data.total * 100, 10);
if($p.length==0){
$("<p class='uploadImgLoad'>上传中... "+progress+"%</p>").appendTo($(this).parent());
}else{
console.info(progress);
$p.text('上传中... '+progress+'%');
if(progress==100){
$p.fadeOut("slow",function(){
$(this).remove();
});
}
}
}).on('fileuploaddone', function (e, data) {
if(data.result.result=='error'){
$(this).removeAttr("disabled");
alert('抱歉,上传过快,请稍等!');
}else if(data.result.result=='success'){
$(this).parent().prepend($("<a href='#' > 删除</a>").attr("onclick","uploadImageAjaxDelete('image_ajax_delete.action?dbFileName="+data.result.dbFileName+"',this)").add("<br/>"))
.prepend($("<img width='140' height='90' border='0' />").attr("src",data.result.url))
.prepend($("<input type='hidden' name="+data.result.hiddenName+" id="+data.result.hiddenName+" value='"+data.result.dbFileName+"' />"));
}
}); }); </script>

3效果:见附件图片。

4 注意:操作的时候一定查看:API,Demo 
https://github.com/blueimp/jQuery-File-Upload/wiki/API 
http://blueimp.github.io/jQuery-File-Upload/basic.html

此外 fireFox 的 debug插件配合使用,有脚本输出的断点功能,以及console.info的显示。

jquery jQuery-File-Upload 例子的更多相关文章

  1. jquery ajax file upload NET MVC 无刷新文件上传

    网上有各种各样的文件上传方法,有基于JS框架的.也有基于flash swf插件的. 这次分享一个比较简单而且实用能快速上手的文件上传方法,主要步骤: 1.引用Jquery包,我用的是jquery-1. ...

  2. 定制jQuery File Upload为微博式单文件上传

    日志未经声明,均为AlloVince原创.版权采用『 知识共享署名-非商业性使用 2.5 许可协议』进行许可. jQuery File Upload是一个非常优秀的上传组件,主要使用了XHR作为上传方 ...

  3. jQuery File Upload 单页面多实例的实现

    jQuery File Upload 的 GitHub 地址:https://github.com/blueimp/jQuery-File-Upload 插件描述:jQuery File Upload ...

  4. jQuery File Upload done函数没有返回

    最近在使用jQuery File Upload 上传图片时发现一个问题,发现done函数没有callback,经过一番折腾,找到问题原因,是由于dataType: ‘json’造成的,改为autoUp ...

  5. 用jQuery File Upload做的上传控件demo,支持同页面多个上传按钮

    需求 有这么一个需求,一个form有多个文件要上传,但又不是传统的图片批量上传那种,是类似下图这种需求,一开始是用的swfupload做的上传,但是问题是如果有多个按钮的话,就要写很多重复的代码,于为 ...

  6. jquery file upload 文件上传插件

    1. jquery file upload 下载 jquery file upload Demo 地址:https://blueimp.github.io/jQuery-File-Upload/ jq ...

  7. jQuery File Upload跨域上传

    最近在做一个一手粮互联网项目,方案为前后端分离,自己负责前端框架,采用了Requirejs+avalonjs+jquery三个框架完成. 前后端通过跨域实现接口调用,中间也发现了不少问题,尤其是在富文 ...

  8. jquery ajax发送delete(use in jquery file upload delete file)

    环境: jQuery file upload HTML example code <div class="pic-preview"> <div class=&qu ...

  9. jquery file upload 后台收到的文件名中文乱码, filename中文乱码

    在jQuery File Upload.js文件里,在以下这个js中有个成员叫做 _initXHRData, 是一个function, 在这个function的最后部分有一个if-else分支,如下:

  10. jQuery File Upload

    jQuery File Upload介绍.............................................. 2 实现基本原理......................... ...

随机推荐

  1. --自动创建备份SQL

    --自动创建备份SQL DECLARE @dbname VARCHAR(50) ,--要备份的数据库名称 @bakname VARCHAR(50) ,--备份后的bat名称 @sql VARCHAR( ...

  2. 关于如何在MVC中 执行JS

    Response.Write("<script>KHTPREFERENCE()</script>"); return this.MessageResult( ...

  3. 不安装Oracle客户端使用PLSQL连接Oracle数据库的方法

    1,下载PL\SQL http://dl8.cr173.com/soft1/PLSQLDeveloper10_ha.zip(这个是我下载的,带破解和汉化); 2,下载完后傻瓜式安装 ,这里说下,1是P ...

  4. struts-json-plugin result中配置对象的序列化

    注意: 在使用strtus-json-plugin序列化对象属性,配置result标签下includeProperties属性时使用 对象名.*或则指定属性名的方式,不能光写对象属性名. 问题: ac ...

  5. IOS网络第二天 - 09-多值参数

    *********** #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @interface ...

  6. htmlentities,html_entity_decode,addslashes

    PHP htmlspecialchars_decode() 函数 PHP htmlspecialchars() 函数 PHP html_entity_decode() 函数 PHP中混淆的三组函数总结 ...

  7. Thinking in Java——笔记(12)

    Error Handling with Exceptions The ideal time to catch an error is at compile time, before you even ...

  8. 简单的oracle分页语句

    SELECT * FROM ( SELECT rownum rn,te.* FROM (SELECT * FROM  tb_enterprise) te  WHERE rownum <= 10) ...

  9. 从Nginx的Web请求处理机制中剖析多进程、多线程、异步IO

    Nginx服务器web请求处理机制 从设计架构来说,Nginx服务器是与众不同的.不同之处一方面体现在它的模块化设计,另一方面,也是最重要的一方面,体现在它对客户端请求的处理机制上. Web服务器和客 ...

  10. rpm与yum

    设置本地源与网络源:https://my.oschina.net/u/1861462/blog/723958 rpm命令:http://man.linuxde.net/rpm https://my.o ...