我尝试允许用户使用以下服务器端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()
。有没有人知道下载失败的原因?