将Json拖放到Chrome中

ita*_*arb 3 javascript html5 google-chrome

我正在寻找一种方法将json文件拖放到chrome中并提取它的信息.

ebi*_*del 9

您可以结合使用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