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?

5
  • 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. Commented Oct 22, 2015 at 19:09
  • Do you mean you want to "upload" the JSON object as a file on a server using javascript? Commented Oct 22, 2015 at 19:12
  • I see. Thank you. So basically call a codebehind c# method on buttonclick where I pass in the data variable. Correct? Commented Oct 22, 2015 at 19:12
  • OOPS, I totally meant to say "client side".. Sorry. Commented Oct 22, 2015 at 19:13
  • This ended up working: stackoverflow.com/questions/13464878/… Commented Oct 22, 2015 at 20:02

2 Answers 2

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

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

7 Comments

I would prefer to do it CodeInPage. What would be the approach to doing this?
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.
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.
Ohhh, yeh you can do that!
|
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...

1 Comment

Does this work on all browsers? Doesn't seem to be working in IE.

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.