Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

heartbeatsfeeling/uploadFile

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

47 Commits

Repository files navigation

jQuery异步上传文件插件v0.9

兼容

兼容IE6+、Firefox、chrome等主流浏览器

说明

本插件优点
  • 1、支持html5模式上传(IE9及以上)
  • 2、支持单文件和多文件上传(IE10及以上)
  • 3、兼容常见主流浏览器(非HTML5模式兼容IE6+)
本插件缺点
  • 1、对错误处理支持不够好,统一放在了timeoutFn函数里
  • 2、测试不足,如果在使用中发现问题,请及时提交issues

使用方法

前端:

html:
单文件:
<input type="file" name="file" id="fileToUpload">
多文件:
<input type="file" name="file" id="fileToUpload" multiple="multiple">

js

	$('#fileToUpload').bind('change',function(){
		var $this = $(this);
		$this.uploadFile({
			url: '/test',//上传路径
			html5Mode:true,//是否开启Html5模式,开启后如果浏览器支持window.FormData,则自动切换到HTML5模式上传文件,默认为false
			filter: function($element) {//上传限制函数
				/*
					上传开始时,会触发该函数
					返回true可以上传,返回false则禁止上传
					例如只可以上传gif
					return $(element).val().indexOf('.gif')!==-1
				*/
				return true;
			},
			timeout:10000,//设置上传超时时间,单位为毫秒
			timeoutFn:function($element){//超时函数
				/*
					如果设置了timeout同时上传超时,会触发该函数
				*/
			},
			complete:function($element){//上传完成
				/*
					上传结束(不管成功还是失败)会触发该函数
				*/
			},
			progress: function(data,$element) {//上传过程中
				/*
					上传过程中触发该函数
				*/
				if(data.html5Mode){//是否开启同时支持html5上传模式
					//Math.round(data.loaded/data.total) 上传进度
				}else{
				}
			},
			success: function(data,$element) {//上传成功
				/*
					上传成功会触发该函数
					data为后台返回的json格式字符串
					插件会自动字data转成Object
				*/
			}
		})
	});

后端:

返回字符串或JSON格式的字符串 :res.end("{'code':'1','id':'imgID','src':'test.jpg'}");

注意:

  • 1、上传过程中,会在input:file外套上form(css属性width:100%,display:inline),上传成功后会自动删除。
  • 2、后台返回数据应为字符串或JSON格式的字符串(如果是json格式的字符串,插件会帮你解析好格式),不要返回纯JSON类型数据,因为IE低版会对form提交返回的纯json类型数据做下载处理。

License

MIT license

About

async upload file by iframe(js异步上传文件)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

AltStyle によって変換されたページ (->オリジナル) /