c# - HttpResponse文件下载不起作用 - "文件无法下载"

时间:2015-07-20 18:32:17

标签: c#

我尝试允许用户使用以下服务器端C#代码下载文件:

public static HttpResponse ExportDataToUser(string settings)
        {
            string filename = "CurrentSettings.txt";
            System.IO.StringWriter sw = new System.IO.StringWriter(new StringBuilder(settings));
            //Open a memory stream that you can use to write back to the response
            byte[] byteArray = Encoding.ASCII.GetBytes(sw.ToString());
            MemoryStream s = new MemoryStream(byteArray);
            StreamReader sr = new StreamReader(s, Encoding.ASCII);
            //Write the stream back to the response
            HttpContext.Current.Response.BufferOutput = true;
            HttpContext.Current.Response.Write(sr.ReadToEnd());
            HttpContext.Current.Response.ContentType = "text/plain";
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + filename);
            HttpContext.Current.Response.Flush();
            //sr.Close();
            //sr.Dispose();
            HttpContext.Current.Response.Close();
            return HttpContext.Current.Response;
        }

当方法到达Flush行时,会出现屏幕底部的下载栏,但是当我点击Open时,系统会提示我"文件无法访问dataType: "text/plain"。下载。"调用此方法的客户端调用具有.getClass()。有没有人知道下载失败的原因?

0 个答案:

没有答案