简体   繁体   English

如何将json对象保存为文件服务器端?

[英]How to save json object as a file server side?

I am calling a Web API using ajax and saving the returned json data object in a variable. 我正在使用ajax调用Web API,并将返回的json数据对象保存在变量中。 How would I download the data to a file client side? 如何将数据下载到文件客户端? Could I do it using codebehind, or within javascript itself? 我可以使用codebehind还是在javascript本身中进行操作?

Really depends on your preference/purpose, you could do it in either CodeInPage or CodeBehind. 确实取决于您的喜好/用途,您可以在CodeInPage或CodeBehind中进行操作。

In code in page you would likely do some ajax command, either in straight javascript or jquery. 在页面代码中,您可能会用直接的javascript或jquery执行一些ajax命令。

Or if you are choosing to do it in CodeBehind by using WebClient. 或者,如果您选择使用WebClient在CodeBehind中进行操作。

EDIT: I overlooked the fact that saving to the server is needed, has to be some sort of CodeBehind then. 编辑:我忽略了一个事实,即需要保存到服务器,必须是某种CodeBehind。

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 编辑2:实际需要的是从网页上下载的能力,请参考此将json对象写入javascript中的文本文件

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... 这是我在其他地方使用过的代码片段,效果很好...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM