gpt4 book ai didi

c# - 通过 GET 下载文件并返回 referrer url

转载 作者:太空宇宙 更新时间:2023-11-03 19:51:47 25 4
gpt4 key购买 nike

我通过 ajax 发送请求,如果响应数据成功,我在 ajax 成功函数中发出 GET 请求:

success: function (data, status) {

if (!data["Success"]) {
alert("Error occurred: Cant read data properly," + data["Message"]);
return null;
}
window.location = '/Home/downloadanddelete?file=output.' + data["filetype"];

问题是当 Get 请求发布到 Controller 时,响应是: enter image description here

如你所见,文件请求url为:“http://localhost:53091/Home/downloadanddelete?file=output.xml

我希望下载这个“output.xml”文件并返回引荐页面 url。

这里是 Controller 中的下载方法:

[HttpGet]
public ActionResult downloadanddelete(string file)
{
string fullName = Path.Combine(HttpRuntime.AppDomainAppPath, "App_Data", file);
if (System.IO.File.Exists(fullName))
{
return File(fullName, "application/xml");
}
return View("Index");
}

这里有什么问题吗?

最佳答案

您需要更改两件事。在服务器 代码中,您需要发送一个Content-Disposition header 来指示该内容是一个“附件”。在发送文件之前添加这些行:

var cd = new System.Net.Mime.ContentDisposition
{
FileName = "filename.xml",
Inline = false
};
Response.AppendHeader("Content-Disposition", cd.ToString());

其次,您应该使用 window.location.assign(...); 而不是设置 window.location 以获得更无缝的浏览器体验。

关于c# - 通过 GET 下载文件并返回 referrer url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38285674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com