尝试用c#作为附件发送文本文件只需发送数据,但不下载任何文件

尝试用c#作为附件发送文本文件只需发送数据,但不下载任何文件,c#,asp.net,C#,Asp.net,我希望能够下载保存在服务器某处的文本文件 我正在尝试将文件作为附件发送,如下所示: public class Download: IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.Clear(); context.Response.ContentType = "text/plain"; context.Response.AddHeader("Conte

我希望能够下载保存在服务器某处的文本文件

我正在尝试将文件作为附件发送,如下所示:

public class Download: IHttpHandler
{
  public void ProcessRequest(HttpContext context)
  {
    context.Response.Clear();
    context.Response.ContentType = "text/plain";
    context.Response.AddHeader("Content-Disposition", "attachment; filename=YourData.txt");
    context.Response.WriteFile(@fullPath); //use your file path here.
    context.Response.Flush();
    context.Response.End();
    ...
return $.ajax({
  method: 'POST',
  url: 'http://' + location.host + '/Download.ashx',
  data: senddata
});
调用此函数的方法是使用以下简单的ajax调用:

public class Download: IHttpHandler
{
  public void ProcessRequest(HttpContext context)
  {
    context.Response.Clear();
    context.Response.ContentType = "text/plain";
    context.Response.AddHeader("Content-Disposition", "attachment; filename=YourData.txt");
    context.Response.WriteFile(@fullPath); //use your file path here.
    context.Response.Flush();
    context.Response.End();
    ...
return $.ajax({
  method: 'POST',
  url: 'http://' + location.host + '/Download.ashx',
  data: senddata
});
如果我尝试使用此选项,则不会发生可见的更改。尽管如此,我的数据确实是从服务器发送的,我可以从chromes的控制台确认这一点。更重要的是,响应头如下所示

Cache-Control:private
Connection:Close
Content-Disposition:attachment; filename=YourData.txt
Content-Type:text/plain
Date:Mon, 25 Jan 2016 09:17:15 GMT
Server:ASP.NET Development Server/10.0.0.0
Transfer-Encoding:chunked
X-AspNet-Version:4.0.30319

为什么我的文件没有作为.txt附件下载?

尝试使用“应用程序/八位字节流”的内容类型。浏览器将其视为下载文件。

检查文件路径,在我的情况下,从服务器尝试时,“路径”是主要问题。您确定它是发送的,因为响应标题中的内容类型是text/html,但在响应内容类型中,您写的是text/plain。@mybirthname首先。。。很好的用户名。是的,我确定,但我忘了在我的测试中,我使用了一个来自不同配置的头,我会更新它。嗨,Mayur,问题是我可以在chrome中看到我的响应中的数据,如果你打印它,变量的值也确实是正确的。你能给我们看一下代码吗,你在哪里将文本写入响应流?@Serv对此表示抱歉,代码在那里,但我在上次编辑时错误地删除了它。这恐怕没有任何效果。请尝试“应用程序/下载”,如果不起作用,在谷歌搜索后,听起来你应该使用隐藏表单提交下载文件的请求,你可以在这些链接中找到它:,恐怕应用程序/下载没有任何作用。但你的第三个链接是有希望的,我会尽快测试它,我找到时间。