I am calling a Web API using ajax and saving the returned json data object in a variable. How would I download the data to a file client side? Could I do it using codebehind, or within javascript itself?
2 Answers
Really depends on your preference/purpose, you could do it in either CodeInPage or CodeBehind.
In code in page you would likely do some ajax command, either in straight javascript or jquery.
Or if you are choosing to do it in CodeBehind by using WebClient.
EDIT: I overlooked the fact that saving to the server is needed, has to be some sort of CodeBehind then.
EDIT 2: Actually what was needed was the ability to download from the webpage, reference this Writing a json object to a text file in javascript
7 Comments
Hope this helps you find what you're looking to achieve...
if(document.getElementById('some-id') != null)
{
document.getElementById('download-button').onclick = function(code) {
this.href = 'data:text/plain;charset=utf-8,'+ encodeURIComponent(JSON.stringify(JSON.parse(yourJSON, null, "\t"));
};
}
It's a snippet I've used elsewhere, works well...
sesssionStorage, so that each client only needs to make the call once (or every 5 minutes, or hour, or X time frame). I don't really know exactly what you're doing, so it depends on your exact use case.