且听风吟

ASP.NET学习中

导航

文件下载上传实现

 这是早一段时间刚学ASP.NET时在CSDN上问的一个问题

为了便于管理上传文件,除分类存储外,拟用更改扩展名的方法:

        如上传文件foo.doc,上传后存储在服务器上改名为foo.doc.kyc,这样可以一定程度上避免病毒感染,而且可以在ASP.NET对.kyc文件进行有效的统一控制。

        下载文件时,将foo.doc.kyc重新改名成foo.doc,使用户不会察觉改名这一过程。

        具体实现方法如下:

   System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;
   System.Web.HttpServerUtility Server = System.Web.HttpContext.Current.Server;

   string filename = "test.zip";
   string filePath = Server.MapPath("") + "/" + filename + ".tst";

   FileInfo file = new FileInfo(filePath);
   if(!file.Exists)
    throw new Exception("没有找到文件.");

   Response.Clear();
   Response.AppendHeader("Content-disposition", "attatchment;filename=" + filename.Replace(".aspx", ""));
   Response.ContentType = "application/octet-stream";
   Response.WriteFile(file.FullName);
   Response.End();

posted on 2004-04-18 10:27  且听风吟  阅读(1856)  评论(1编辑  收藏  举报