C#下载文件 - BADRESPONSE:无效或意外的令牌

时间:2017-05-05 12:22:50

标签: c#

我正在调用这个直接方法但是当我尝试下载文件时遇到此错误(路径正常并经过测试):

[DirectMethod]
    public void downloadFile(string fname, string ftype)
    {
        HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STREAM";
        String Header = "Attachment; Filename=" + fname;
        HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
        System.IO.FileInfo Dfile = new System.IO.FileInfo(HttpContext.Current.Server.MapPath("CopyFiles\\" + ftype + "\\"+ fname));
        HttpContext.Current.Response.WriteFile(Dfile.FullName);
        HttpContext.Current.Response.End();
    }
  

BADRESPONSE:无效或意外的令牌

1 个答案:

答案 0 :(得分:0)

只需要在WriteFile()之前刷新():

[DirectMethod]
public void downloadFile(string fname, string ftype)
{
    HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STREAM";
    String Header = "Attachment; Filename=" + fname;
    HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
    System.IO.FileInfo Dfile = new System.IO.FileInfo(HttpContext.Current.Server.MapPath("CopyFiles\\" + ftype + "\\"+ fname));
    HttpContext.Current.Response.Flush();
    HttpContext.Current.Response.WriteFile(Dfile.FullName);
    HttpContext.Current.Response.End();
}