1
2
//下载文件
3 public void DownLoad(string url){
4 url=ConvertServerPath(url);
6 string filePath =Server.MapPath(url);
7 FileInfo fileinfo =new FileInfo ();
8 Response.Clear();
9 Response.ClearContent();
10 Response.ClearHeaders();
11 Response.AddHeader("Content-Disposition","attachment;filename"+fileInfo.Name);
12 Response.AddHeader("Content-Length", fileInfo.Length.ToString());
13 Response.AddHeader("Content-Transfer-Encoding", "binary");
14 Response.ContentType = "application/octet-stream";
15 Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
16 Response.WriteFile(fileinfo.FullName);
17 Response.Flush();
18 Response.End();
19
20 }
21
22
23 //转换成网站路径
24 public string ConvertServerPath(string url){
25 string mapPath =HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath.ToString()); //系统根路径
28 string xdPath=url.Replace(mapPath,"~"); //网站路径
30 xdPath=xdpath.Replace(@"",@"/");
31 return xdPath;
3 }
//上传文件
public void UpLoad(FileUpload upload){
if(upload.HasFile){ //存在文件
if(upload.PostedFile.ContentLength< 100000){ //判断文件大小
string fullName =upload.PostedFile.FileName; //获取文件全称
string fileName =fullName.Substring(fullName.LastIndexOf("\")+1); //获取文件全称
string serverpath=Server.MapPath("~//File//")+fileName; //存放到服务器路径
upload.PostedFile.SaveAs(serverpath);
}
}
}