一丶下载服务器图片

1 static void Main(string[] args) 2 { 3 try 4 { 5 qds107762222_dbEntities db = new qds107762222_dbEntities(); 6 var zucai_Team = db.Set<zucai_team>().ToList(); 7 foreach (var item in zucai_Team) 8 { 9 if (!String.IsNullOrEmpty(item.ImgPath)) 10 { 11 string[] str = item.ImgPath.Split(new char[] { '/' }); 12 var imgName = str[str.Length - 1]; 13 Console.WriteLine(item.ImgPath); 14 15 string path = "G:\img1\" + imgName; 16 WebClient web = new WebClient(); 17 if (!File.Exists(path)) 18 { 19 web.DownloadFile(item.ImgPath, path); 20 } 21 } 22 23 } 24 25 } 26 catch (Exception ex) 27 { 28 29 throw ex; 30 } 31 Console.WriteLine("ok"); 32 Console.ReadKey(); 33 }
二丶下载文件

1 public ActionResult DownloadFile(string fileRelativePath) 2 { 3 //1.根据文件相对路径获取文件绝对路径 4 string fileAbsolutelyPath = Server.MapPath("~" + fileRelativePath); 5 //2.获取文件信息 6 FileInfo file = new FileInfo(fileAbsolutelyPath ); 7 8 if (file.Exists) 9 { 10 //3.获取指定文件名的 MIME 映射 11 string contentType = MimeMapping.GetMimeMapping(fileAbsolutelyPath ); 12 //4.下载文件 13 return File(objFileBasePath, contentType, file.Name); 14 } 15 else 16 { 17 return Content("文件不存在"); 18 } 19 20 }