我已经成功使用以下方法一段时间了,但是出现了一个问题。
public string downloadfiletoclient()
{
string SessionId = HttpContext.Current.Session["SessionVar_ID"].ToString();
Response.Clear();
string outPath = "";
if (!Directory.Exists(HttpContext.Current.Server.MapPath("~/User_Generated_Content/" + SessionId)))
{
return "-1";
}
var directory = new DirectoryInfo(HttpContext.Current.Server.MapPath("~/User_Generated_Content/" + SessionId));
var fileName = directory.GetFiles().OrderByDescending(f => f.LastWriteTime).First();
outPath = HttpContext.Current.Server.MapPath("~/User_Generated_Content/" + SessionId + "/" + fileName.ToString());
Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
}
Response.WriteFile(outPath);
Response.End();
return "1";
该方法现在不适用于我生成的png文件。它曾经工作,但我已经切换到生成图像的新控件。该方法仍适用于.xls文件。我不知道发生了什么事。不会生成任何错误消息。事情看起来不错。只是文件donwload永远不会在浏览器中启动。
请帮助,我不知道该怎么做。
答案 0 :(得分:0)
我不太清楚为什么只有png文件的问题,这有点奇怪。但是,我建议你调试你的应用程序,看看究竟发送给客户端的是什么。此外,由于您将文件“附加”到响应中,因此我还将内容类型设置为八位字节流。试一试
答案 1 :(得分:0)
如上所述,Response.ContentType = "application/octet-stream"
可能有所帮助......