最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using HTML 5 File API to load a JSON file - Stack Overflow

programmeradmin4浏览0评论

I want the user to be be able to chose a JSON file on there puter, this JSON file should then be made available to the client side Javascript.

How would I do this using the FILE API, the ultimate goal is the the user selected JSON file to be available as an object which I can then play about with in Javascript. This is what I have so far:

JsonObj = null 



function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object
     f = files[0];
      var reader = new FileReader();

      // Closure to capture the file information.
      reader.onload = (function(theFile) {
        return function(e) {
          // Render thumbnail.
         JsonObj = e.target.result
         console.log(JsonObj);
        };
      })(f);

      // Read in the image file as a data URL.
      reader.readAsDataURL(f);
    }



document.getElementById('files').addEventListener('change', handleFileSelect, false);

FIDDLE: /

How would I convert the variable JsonObj to a proper Json object, one can can add new fields to etc.

I want the user to be be able to chose a JSON file on there puter, this JSON file should then be made available to the client side Javascript.

How would I do this using the FILE API, the ultimate goal is the the user selected JSON file to be available as an object which I can then play about with in Javascript. This is what I have so far:

JsonObj = null 



function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object
     f = files[0];
      var reader = new FileReader();

      // Closure to capture the file information.
      reader.onload = (function(theFile) {
        return function(e) {
          // Render thumbnail.
         JsonObj = e.target.result
         console.log(JsonObj);
        };
      })(f);

      // Read in the image file as a data URL.
      reader.readAsDataURL(f);
    }



document.getElementById('files').addEventListener('change', handleFileSelect, false);

FIDDLE: http://jsfiddle/jamiefearon/8kUYj/

How would I convert the variable JsonObj to a proper Json object, one can can add new fields to etc.

Share Improve this question edited Oct 26, 2021 at 23:08 ted 14.7k10 gold badges68 silver badges113 bronze badges asked Feb 6, 2013 at 23:33 Jamie FearonJamie Fearon 2,63413 gold badges49 silver badges65 bronze badges 3
  • maybe this will help. html5rocks./en/tutorials/file/dndfiles – Zach Lysobey Commented Feb 6, 2013 at 23:37
  • I have read the tutorial you have mentioned and have updated my answer to show you what I have got. – Jamie Fearon Commented Feb 6, 2013 at 23:46
  • You've got a small syntax bug in your example. There's no semicolon after JsonObj = e.target.result (in the inner-most code block) – Grant Peters Commented Jul 22, 2013 at 5:51
Add a ment  | 

1 Answer 1

Reset to default 12

Don't load the data as a "DataUrl" via readAsDataURL, instead use readAsText then parse it via JSON.parse()

e.g.

JsonObj = null 

function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object
     f = files[0];
      var reader = new FileReader();

      // Closure to capture the file information.
      reader.onload = (function(theFile) {
        return function(e) {
          // Render thumbnail.
         JsonObj = JSON.parse(e.target.result);
         console.log(JsonObj);
        };
      })(f);

      // Read in the image file as a data URL.
      reader.readAsText(f);
    }

document.getElementById('files').addEventListener('change', handleFileSelect, false);
发布评论

评论列表(0)

  1. 暂无评论