public static void HandleFiles(List <VisitFile> needdels, List <VisitFile> neednews, List <string> oldFiles, string visitID, string type, Dictionary <string, string> copyfiles, DBEntities db) { var rootpath = ConfigurationManager.AppSettings["rootpath"].ToString(); if (needdels != null) { foreach (var item in needdels) { if (File.Exists(System.IO.Path.Combine(rootpath, item.FileUrl)) && !oldFiles.Contains(item.FileUrl)) { oldFiles.Add(System.IO.Path.Combine(rootpath, item.FileUrl)); } db.Entry(item).State = System.Data.Entity.EntityState.Deleted; } } if (neednews != null) { foreach (var item in neednews) { if (!string.IsNullOrEmpty(item.FileUrl)) { try { if (File.Exists(System.IO.Path.Combine(rootpath, item.FileUrl))) { var curMonth = DateTime.Now.ToString("yyyyMM"); if (!Directory.Exists(System.IO.Path.Combine(rootpath, curMonth))) { Directory.CreateDirectory(System.IO.Path.Combine(rootpath, curMonth)); } var newpath = item.FileUrl.Replace("temp\\", curMonth + "\\"); if (!copyfiles.ContainsKey(System.IO.Path.Combine(rootpath, item.FileUrl))) { copyfiles.Add(System.IO.Path.Combine(rootpath, item.FileUrl), System.IO.Path.Combine(rootpath, newpath)); } item.FileUrl = newpath; VisitFile visitFile = new VisitFile(); visitFile.FileID = Guid.NewGuid().ToString("N"); visitFile.FileUrl = item.FileUrl; visitFile.FileName = item.FileName; visitFile.OutID = visitID; visitFile.Type = type; db.VisitFiles.Add(visitFile); } } catch (Exception ex) { LogHelper.WriteError(ex); } } } } }
public async Task <HttpResponseMessage> Download(string id) { return(await Task.Run <HttpResponseMessage>(() => { HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); VisitFile visitFile = null; using (var db = new DBEntities()) { visitFile = db.VisitFiles.Where(t => t.FileID == id).FirstOrDefault(); } var rootpath = ConfigurationManager.AppSettings["rootpath"].ToString(); if (visitFile != null) { var filePath = System.IO.Path.Combine(rootpath, visitFile.FileUrl); if (File.Exists(filePath)) { var stream = File.OpenRead(filePath); result.Content = new StreamContent(stream); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); result.Content.Headers.Add("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(visitFile.FileName) + "\""); return result; } } else if (visitFile == null) { var filePath = System.IO.Path.Combine(rootpath, id + ".xls"); var filezipPath = System.IO.Path.Combine(rootpath, id + ".zip"); if (File.Exists(filezipPath)) { var stream = File.OpenRead(filezipPath); result.Content = new StreamContent(stream); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); result.Content.Headers.Add("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(id) + ".zip\""); return result; } else if (File.Exists(filePath)) { var stream = File.OpenRead(filePath); result.Content = new StreamContent(stream); result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/ms-excel"); result.Content.Headers.Add("Content-Disposition", "attachment;filename=\"" + HttpUtility.UrlEncode(id) + ".xls\""); return result; } } throw new HttpException(404, "无效文件"); })); }