文件下载上传实现
为了便于管理上传文件,除分类存储外,拟用更改扩展名的方法:
如上传文件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();