up.js
1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
(function($){
function Upload__(option){
this.option = option = $.extend({},Upload__.default,option)
this.bind()
}
Upload__.default ={
el:"",
url:"",
isImg:true,
success:function(data){
},
type:"post",
}
Upload__.prototype = {
bind:function(){
if(!this.option.url || !this.option.el){
console.error("no url or el")
return false
}
$(this.option.el).click(function(){
$("#ffffff").remove()
$("body").append("<form id='ffffff'><input type='file' name='file' "+ (this.option.isImg? "accept='image/*'" : "") +" style='display:none'></from>")
$("#ffffff input").click()
}.bind(this))
var that = this
$(document).on("change","#ffffff input",function(){
that.send()
})
},
send:function(){
var data = new FormData($("#ffffff")[0])
$.ajax({
url: this.option.url,
type: this.option.type,
data: data,
asyne:false,
processData: false,
contentType: false,
success:this.option.success,
beforeSend:function(){
$("#ffffff").remove()
}
});
},
}
$.extend({
up:function(op){
new Upload__(op)
}
})
})(jQuery)