Movatterモバイル変換


[0]ホーム

URL:


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
This repository was archived by the owner on May 16, 2018. It is now read-only.

Commitf14fa32

Browse files
committed
Merge pull request#12 from theoephraim/master
Allow user to trigger upload start by passing in an HTML5 file drop event
2 parents31c339b +fb82d7c commitf14fa32

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

‎example.html

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,17 @@
44
<scriptsrc="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"type="text/javascript"></script>
55
<scriptsrc="jquery.html5_upload.js"type="text/javascript"></script>
66
<metacontent="text/html; charset=utf-8"http-equiv="Content-Type"/>
7+
8+
<style>
9+
#drop_area.file_hover {
10+
background:red;
11+
}
12+
13+
</style>
714
</head>
815
<body>
916
<inputtype="file"multiple="multiple"id="upload_field"/>
17+
<divid="drop_area"style="margin:20px 0; width:300px; padding:20px; border:1px dashed black;">or drop file(s) here</div>
1018
<divid="progress_report">
1119
<divid="progress_report_name"></div>
1220
<divid="progress_report_status"style="font-style: italic;"></div>
@@ -16,7 +24,7 @@
1624
</div>
1725
<scripttype="text/javascript">
1826
$(function(){
19-
$("#upload_field").html5_upload({
27+
var$input=$("#upload_field").html5_upload({
2028
url:function(number){
2129
returnprompt(number+" url","/");
2230
},
@@ -44,6 +52,28 @@
4452
alert('error while uploading file '+name);
4553
}
4654
});
55+
56+
57+
58+
// Example of how to use this with a html5 drop event
59+
60+
// this is absolutely necessary -- see http://weblog.bocoup.com/using-datatransfer-with-jquery-events/
61+
$.event.props.push('dataTransfer');
62+
63+
var$drop=$('#drop_area');
64+
$drop.on('dragover dragenter',function(e){
65+
$drop.addClass('file_hover');
66+
returnfalse;
67+
}).on('dragleave dragexit',function(e){
68+
$drop.removeClass('file_hover');
69+
returnfalse;
70+
}).on('drop',function(e){
71+
if(e.originalEvent.dataTransfer&&e.originalEvent.dataTransfer.files.length){
72+
$input.trigger('html5_upload.startFromDrop',e);
73+
}
74+
returnfalse;
75+
})
76+
4777
});
4878
</script>
4979
</body>

‎jquery.html5_upload.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
(function($){
2-
jQuery.fn.html5_upload=function(options){
2+
$.fn.html5_upload=function(options){
3+
34
functionget_file_name(file){
45
returnfile.name||file.fileName;
56
}
67
functionget_file_size(file){
78
returnfile.size||file.fileSize;
89
}
910
varavailable_events=['onStart','onStartOne','onProgress','onFinishOne','onFinish','onError'];
10-
varoptions=jQuery.extend({
11+
varoptions=$.extend({
1112
onStart:function(event,total){
1213
returntrue;
1314
},
@@ -78,8 +79,7 @@
7879
}
7980
},options);
8081

81-
functionupload(){
82-
varfiles=this.files;
82+
functionupload(files){
8383
vartotal=files.length;
8484
var$this=$(this);
8585
if(!$this.triggerHandler('html5_upload.onStart',[total])){
@@ -210,12 +210,15 @@
210210

211211
try{
212212
returnthis.each(function(){
213+
varfile_input=this;
213214
this.html5_upload={
214215
xhr:newXMLHttpRequest(),
215216
continue_after_abort:true
216217
};
217218
if(options.autostart){
218-
$(this).bind('change',upload);
219+
$(this).bind('change',function(e){
220+
upload.call(e.target,this.files);
221+
});
219222
}
220223
varself=this;
221224
$.each(available_events,function(event){
@@ -224,6 +227,11 @@
224227
}
225228
});
226229
$(this)
230+
.bind('html5_upload.startFromDrop',function(e,dropEvent){
231+
if(dropEvent.dataTransfer&&dropEvent.dataTransfer.files.length){
232+
upload.call(file_input,dropEvent.dataTransfer.files);
233+
}
234+
})
227235
.bind('html5_upload.start',upload)
228236
.bind('html5_upload.cancelOne',function(){
229237
this.html5_upload['xhr'].abort();
@@ -245,4 +253,4 @@
245253
returnfalse;
246254
}
247255
};
248-
})(jQuery);
256+
})(jQuery);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp