6

I need to save a graph to a JSON file. Before doing that, I wrote a very simple HTML/JavaScript to test it out (just use a simple string for the JSON).

Based on the suggestion in a Stack Overflow posting, I use saveAs() method. But it looks like saveAs() does not work and no file is created. I am using Internet Explorer and Window 10.

In order to debug it, I inserted 3 window.alert() calls. The first 2 window.alert() calls pop up correctly, however, the 3rd window.alert() call does not show up at all. So I am afraid that the code is aborted at the saveAs() call.

The following is my code:

<html>
<head>
<title>Save</title>
<script>

function save() 
{ 
window.alert("I am here 1");
var jsonBlob = new Blob([JSON.stringify("kiki")], { type: 'application/javascript;charset=utf-8' });
window.alert("I am here 2");
saveAs(jsonBlob, "testout.json");
window.alert("I am here 3");
} 

</script> 
</head>
<body> 
<form name="myform"> 
<input type="button" onClick="save();" value="Save">
</form> 
</body>
</html>

I am wondering why saveAs() does not work for me? Am I missing something here? Do I need to add something to my computer in order to use saveAs() method?

Thank you very much for your advice!

5
  • 1
    Are you showing all of your code? You need to define saveAs. Commented Mar 19, 2017 at 3:53
  • 2
    "Based on the suggestion in a Stack Overflow posting" Can you link to the post? saveAs is not a built-in function. Commented Mar 19, 2017 at 3:54
  • Include FileSaver script. saveAs is not a default API, it's a library's command. But note that in IE, you've got msSaveBlob API, which doesn't need a lib (only available in IE). Commented Mar 19, 2017 at 3:54
  • Your browser's console is where you can check for errors in such cases. Commented Mar 19, 2017 at 4:18
  • Thanks a lot everyone! Your advice is very helpful! By including FileSaver.js, it works now. Thanks again! Commented Mar 20, 2017 at 3:24

2 Answers 2

7

You are a little bit error in saveAs. Code is given below:

 <html>
    <head>
    <title>Save</title>
    <script>

    function save() 
    { 
    window.alert("I am here 1");
    var jsonBlob = new Blob([JSON.stringify("kiki")], { type: 'application/javascript;charset=utf-8' });
    window.alert("I am here 2");
   var link=window.URL.createObjectURL(jsonBlob);
    window.location=link;
    window.alert("I am here 3");
    } 

    </script> 
    </head>
    <body> 
    <form name="myform"> 
    <input type="button" onClick="save();" value="Save">
    </form> 
    </body>
    </html>

Also you can see this: http://eligrey.com/demos/FileSaver.js/

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for your advice! By including FileSaver.js, it works now. Thanks again!
@qcx if it is solved your problem,then please accept my answer.
2

Convert into excel sheet using JavaScript

var excelBlob = new Blob([response], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }, "excel.xlsx");     
var link=window.URL.createObjectURL(excelBlob);
window.location=link;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.