html5 的文件 api 示例与 jquery?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4722500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
html5's file api example with jquery?
提问by dr jerry
I would like to use html5's file api, in combination with the external drag n drop functionality (drag an external file onto a designated spot and capture its contents) and jquery. I found a working non-jquery example: (html5 demo: file api)
我想使用 html5 的文件 api,结合外部拖放功能(将外部文件拖到指定位置并捕获其内容)和 jquery。我找到了一个工作的非 jquery 示例:(html5 演示:文件 api)
var drop = document.getElementById('drop');
drop.ondragover = function () {this.className = 'focus'; return false;};
drop.ondragend = function () { this.className = ''; return false; };
drop.ondrop=function(e) {
this.className = '';
e.preventDefault();
var file = e.dataTransfer.files[0];
var reader = new FileReader();
reader.onload = function (evt) {
console.log(evt.target.result);
}
reader.readAsText(file);
};
This works fine. Now I would like to make this more a jquery-ish and I tried:
这工作正常。现在我想让它更像 jquery-ish,我试过:
$("#drop").bind('ondragover',function() {this.addClass('focus'); return false;})
.bind("ondragend",function () { this.removeClass('focus'); return false;})
.bind("ondrop",function(e) {
this.removeClass("focus");
e.preventDefault();
var file = e.dataTransfer.files[0];
var reader = new FileReader();
reader.onload = function (evt) {
console.log(evt.target.result);
}
reader.readAsText(file);
});
But that doesn't work, none of the binded events seems to get triggered. I also tried to loose the "on" part for the eventnames but that also doesn't work. Hopefully somebody here can shine a light?
但这不起作用,似乎没有任何绑定事件被触发。我还尝试松开事件名称的“on”部分,但这也不起作用。希望这里有人能发光?
regards, jeroen.
问候,杰伦。
采纳答案by Gidon
The solution is simple.
解决方法很简单。
- Lose the
on
prefix (that's why no events were thrown) this.
=>$(this).
(that's why when events were thrown nothing happened, it gave an error).
- 丢失
on
前缀(这就是为什么没有抛出事件的原因) this.
=>$(this).
(这就是为什么当事件被抛出时什么也没有发生,它给出了一个错误)。
With me it worked.
对我来说它奏效了。
回答by Jacob
Gidon's description solves the problem. Here is a fully coded example in case someone else is looking to solve this problem and wants more detail.
Gidon 的描述解决了这个问题。这是一个完整编码的示例,以防其他人希望解决此问题并需要更多详细信息。
// Bindings to HTML; replace these with your components.
var $dropArea = $('#dropArea');
var $picsHolder = $('#picsHolder');
// Attach our drag and drop handlers.
$dropArea.bind({
dragover: function() {
$(this).addClass('hover');
return false;
},
dragend: function() {
$(this).removeClass('hover');
return false;
},
drop: function(e) {
e = e || window.event;
e.preventDefault();
// jQuery wraps the originalEvent, so we try to detect that here...
e = e.originalEvent || e;
// Using e.files with fallback because e.dataTransfer is immutable and can't be overridden in Polyfills (http://sandbox.knarly.com/js/dropfiles/).
var files = (e.files || e.dataTransfer.files);
var $img = $('<img src="" class="uploadPic" title="" alt="" />');
for (var i = 0; i < files.length; i++) {
(function(i) {
// Loop through our files with a closure so each of our FileReader's are isolated.
var reader = new FileReader();
reader.onload = function(event) {
var newImg = $img.clone().attr({
src: event.target.result,
title: (files[i].name),
alt: (files[i].name)
});
// Resize large images...
if (newImg.size() > 480) {
newImg.width(480);
}
$picsHolder.append(newImg);
};
reader.readAsDataURL(files[i]);
})(i);
}
return false;
}
});
#dropArea {
border: 10px dashed;
border-radius: 10px;
border-color: gray;
width: 200px;
height: 200px;
}
#dropArea:hover {
border-color: black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="picsHolder"></div>
<div id="dropArea"></div>