0

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?

SKLAK
  • 3,825
  • 9
  • 33
  • 57
  • 2
    To save it server side you'd have to have the code running on the server, so JS won't work. If you're using C#, you could create a function to do the call and write the response to a local file, then read that file each time you need it. Also possibly timestamp it so that you can check if the data is getting too stale. You could also cache it for each client using javascript's `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. – Steven Spasbo Oct 22 '15 at 19:09
  • Do you mean you want to "upload" the JSON object as a file on a server using javascript? – An0nC0d3r Oct 22 '15 at 19:12
  • I see. Thank you. So basically call a codebehind c# method on buttonclick where I pass in the data variable. Correct? – SKLAK Oct 22 '15 at 19:12
  • OOPS, I totally meant to say "client side".. Sorry. – SKLAK Oct 22 '15 at 19:13
  • This ended up working: http://stackoverflow.com/questions/13464878/creating-csv-file-offline-client-side-in-internet-explorer – SKLAK Oct 22 '15 at 20:02

2 Answers2

1

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

Community
  • 1
  • 1
ValDeV
  • 31
  • 4
  • I would prefer to do it CodeInPage. What would be the approach to doing this? – SKLAK Oct 22 '15 at 19:09
  • Ah, I missed the word download here. You are going to need to implement this in CodeBehind. As you cannot save files to a server using javascript. – ValDeV Oct 22 '15 at 19:11
  • Sorry, I'm an idiot. I meant to say client side this whole time. The client calls the API from a button click, gets a json object. And I want the client to be able to download this json object to a file on button click. – SKLAK Oct 22 '15 at 19:14
  • Ohhh, yeh you can do that! – An0nC0d3r Oct 22 '15 at 19:14
  • Reference this http://stackoverflow.com/questions/22055598/writing-a-json-object-to-a-text-file-in-javascript – ValDeV Oct 22 '15 at 19:18
  • Thanks, that's what I was looking for. – SKLAK Oct 22 '15 at 19:19
  • Hmm, doesn't seem to be working in IE. It just takes me to the url that's supposed to download the data but the url is just a 404 – SKLAK Oct 22 '15 at 19:28
0

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...

An0nC0d3r
  • 1,275
  • 13
  • 33