静态方法如何实现文件下载?
[WebMethod]public static int Download(int FID)
{
string fileName = "1.txt";
string filePath = HttpContext.Current.Server.MapPath("~/Upload/Temp/1.txt");
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
上述静态方法,下载文件为什么无反应? --------------------编程问答-------------------- 下载文件其实真和静不静态没多大关系
static void DownloadFile(string Uri, string FileName)
{
new System.Net.WebClient().DownloadFile(Uri, FileName);
}
你需要的静态下载方法,拿去 --------------------编程问答-------------------- webserice 是什么你知道吗?
下载文件还需要webserice吗?
目测代码100%报错。 --------------------编程问答-------------------- 你要用ajax下载啊?有必要吗? --------------------编程问答-------------------- 最简单的方法是你新建一个ashx或者aspx页面,把你的代码放在入口,以后下载的时候新开方法打开这个页面就可以加载了
补充:.NET技术 , ASP.NET