如何在ASP.NET Core中下载.json文件
我在控制器中的下载方法是
public FileResult GenerateJSON()
{
string filename = "aa.json";
string filepath = VariableDeclarations.wwwPath + @"\Downloads\" + filename;
string filedata = System.IO.File.ReadAllText(filepath);
string contentType = GetContentType(filepath);
var cd = new System.Net.Mime.ContentDisposition
{
FileName = filename,
Inline = true
};
HttpContext.Response.Headers.Add("Content-Disposition",$"attachment;filename=aa.json");
return File(filedata, "application/json", filename);
}
且视图代码在按钮上单击
function OnClickbtn() {
$.ajax({
type: "POST",
url: vGenerateUrl,
data: { },
success: function(s) {
DevExpress.ui.notify({ message: "xyz Generated", width: 1300 }, vSuccessMsgType, 3000);
},
error: function(result) {
onAjaxError(result);
}
});
}
按钮单击我的视图,从我的视图中生成.json文件,我需要下载该.JSON文件,应在浏览器中下载,但是在多次努力从ActionResult,ContentResult进行更改之后fileResult我仍然无法在浏览器中下载文件。
我需要在上面的代码中进行哪些更改才能在浏览器中下载.json文件?
My Download Method in Controller is
public FileResult GenerateJSON()
{
string filename = "aa.json";
string filepath = VariableDeclarations.wwwPath + @"\Downloads\" + filename;
string filedata = System.IO.File.ReadAllText(filepath);
string contentType = GetContentType(filepath);
var cd = new System.Net.Mime.ContentDisposition
{
FileName = filename,
Inline = true
};
HttpContext.Response.Headers.Add("Content-Disposition",quot;attachment;filename=aa.json");
return File(filedata, "application/json", filename);
}
and View Code is On button Click
function OnClickbtn() {
$.ajax({
type: "POST",
url: vGenerateUrl,
data: { },
success: function(s) {
DevExpress.ui.notify({ message: "xyz Generated", width: 1300 }, vSuccessMsgType, 3000);
},
error: function(result) {
onAjaxError(result);
}
});
}
On Button click from my view a .json file is generated and i need to download that .json file should be downloaded in browser but after multiple efforts for changing it from actionresult, contentresult and fileresult I am still not able to download a file in my browser.
what changes do I need to make in the above code to download .json file in browser?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是浏览器中的关于下载.json文件的演示,您可以参考它。
在视图中:
结果:
或
任务
结果:
Below is a demo about download .json file in browser, you can refer to it.
in the view:
result:
Or
Mission
result: