您可以结合使用HTML5 Drag&Drop和FileReaderAPI来读取文件:
var dnd = new DnDFileController('body', function(files) {
var f = files[0];
if (!f.type.match('application/json')) {
alert('Not a JSON file!');
}
var reader = new FileReader();
reader.onloadend = function(e) {
var result = JSON.parse(this.result);
console.log(result);
};
reader.readAsText(f);
});
Run Code Online (Sandbox Code Playgroud)
DnDFileController来自http://html5-demos.appspot.com/static/filesystem/filer.js/demos/js/dnd.js,只是在您作为选择器传入的元素上设置正确的DnD事件侦听器.
见http://jsbin.com/oqosav/2/edit
| 归档时间: |
|
| 查看次数: |
3342 次 |
| 最近记录: |