using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.IO; using System.Web.UI; using System.Web.Mvc; using Bowin.Common; using Ionic.Zip; namespace EMIS.Utility { public class FileUploadHelper { private const long BUFFER_SIZE = 4096; public class RemoteFileInfo { public string FileName { get; set; } public string RemotePath { get; set; } } /// /// 查询上传文件对应的FileUploadSessionName /// /// /// public static string GetFileUploadSessionName(Guid formID) { return Const.UPLOAD_SESSION_NAME + "_" + formID.ToString(); } /// /// 查询文件上传对应的TagetPhysicalPath(不带文件名及后缀名) /// /// /// public static string GetFileUploadTagetPhysicalPath(string savePath = null) { string filePath = Const.LOCAL_SETTING_UPLOAD_FILEPATH; string physicalPath = HttpContext.Current.Server.MapPath(filePath); string dayFolderName = DateTime.Today.ToString("yyyy-MM"); string tagetPhysicalPath = (physicalPath.EndsWith("\\") ? physicalPath : physicalPath + "\\") + dayFolderName + "\\"; if (!string.IsNullOrEmpty(savePath)) { filePath += "/" + savePath; physicalPath = HttpContext.Current.Server.MapPath(filePath); tagetPhysicalPath = (physicalPath.EndsWith("\\") ? physicalPath : physicalPath + "\\"); } if (!Directory.Exists(tagetPhysicalPath)) { Directory.CreateDirectory(tagetPhysicalPath); } return tagetPhysicalPath; } /// /// 文件上传 /// savePath是相对与web上传路径的子路径 /// 如:web上传路径为~/Content/DownFile,savePath为SOC/2018-201901,则实际上传的路径为~/Content/DownFile/SOC/2018-201901 /// /// /// /// public static string UploadFileInternal(HttpPostedFileBase file, string savePath = null) { if (file == null || string.IsNullOrEmpty(file.FileName)) return null; UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext); string filePath = Const.LOCAL_SETTING_UPLOAD_FILEPATH; string physicalPath = HttpContext.Current.Server.MapPath(filePath); string dayFolderName = DateTime.Today.ToString("yyyy-MM"); string tagetPhysicalPath = (physicalPath.EndsWith("\\") ? physicalPath : physicalPath + "\\") + dayFolderName + "\\"; if (!string.IsNullOrEmpty(savePath)) { filePath += "/" + savePath; physicalPath = HttpContext.Current.Server.MapPath(filePath); tagetPhysicalPath = (physicalPath.EndsWith("\\") ? physicalPath : physicalPath + "\\"); } if (!Directory.Exists(tagetPhysicalPath)) { Directory.CreateDirectory(tagetPhysicalPath); } var newFileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName); var physicalFileName = tagetPhysicalPath + newFileName; file.SaveAs(physicalFileName); return physicalFileName; } /// /// 文件上传 /// savePath是相对与web上传路径的子路径 /// 如:web上传路径为~/Content/DownFile,savePath为SOC/2018-201901,则实际上传的路径为~/Content/DownFile/SOC/2018-201901 /// /// /// /// /// /// public static string UploadFileInternal(HttpPostedFileBase file, out string fileSourceWebPath, out string errorSourceWebPath, string savePath = null) { fileSourceWebPath = null; errorSourceWebPath = null; if (file == null || string.IsNullOrEmpty(file.FileName)) return null; UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext); string filePath = Const.LOCAL_SETTING_UPLOAD_FILEPATH; string physicalPath = HttpContext.Current.Server.MapPath(filePath); string dayFolderName = DateTime.Today.ToString("yyyy-MM"); string tagetPhysicalPath = (physicalPath.EndsWith("\\") ? physicalPath : physicalPath + "\\") + dayFolderName + "\\"; if (!string.IsNullOrEmpty(savePath)) { filePath += "/" + savePath; physicalPath = HttpContext.Current.Server.MapPath(filePath); tagetPhysicalPath = (physicalPath.EndsWith("\\") ? physicalPath : physicalPath + "\\"); } else { filePath += "/" + dayFolderName; } if (!Directory.Exists(tagetPhysicalPath)) { Directory.CreateDirectory(tagetPhysicalPath); } var newFileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName); var physicalFileName = tagetPhysicalPath + newFileName; fileSourceWebPath = urlHelper.Content(filePath + "/" + newFileName); errorSourceWebPath = urlHelper.Content(filePath + "/" + Guid.NewGuid().ToString() + ".xls"); file.SaveAs(physicalFileName); return physicalFileName; } /// /// 删除FileInternal /// /// public static void DeleteFileInternal(string filePath) { string fullPath = HttpContext.Current.Server.MapPath("~" + filePath); File.Delete(fullPath); } /// /// 文件上传 /// savePath是相对与web上传路径的子路径 /// 如:web上传路径为~/Content/DownFile,savePath为SOC/2018-201901,则实际上传的路径为~/Content/DownFile/SOC/2018-201901 /// /// /// /// public static string UploadFile(HttpPostedFileBase file, string savePath = null) { if (file == null || string.IsNullOrEmpty(file.FileName)) return null; UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext); string filePath = Const.LOCAL_SETTING_UPLOAD_FILEPATH; string dayFolderName = DateTime.Today.ToString("yyyy-MM"); var physicalFileName = UploadFileInternal(file, savePath); if (!string.IsNullOrEmpty(savePath)) { filePath += "/" + savePath; } else { filePath += "/" + dayFolderName; } var webFileName = filePath + "/" + Path.GetFileName(physicalFileName); return urlHelper.Content(webFileName); } /// /// 批量文件上传 /// savePath是相对与web上传路径的子路径 /// 如:web上传路径为~/Content/DownFile,savePath为SOC/2018-201901,则实际上传的路径为~/Content/DownFile/SOC/2018-201901 /// /// /// /// public static List UploadFile(HttpFileCollectionBase fileList, string savePath = null) { UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext); List result = new List(); string filePath = Const.LOCAL_SETTING_UPLOAD_FILEPATH; string physicalPath = HttpContext.Current.Server.MapPath(filePath); string dayFolderName = DateTime.Today.ToString("yyyy-MM"); string tagetPhysicalPath = (physicalPath.EndsWith("\\") ? physicalPath : physicalPath + "\\") + dayFolderName + "\\"; if (!string.IsNullOrEmpty(savePath)) { filePath += "/" + savePath; physicalPath = HttpContext.Current.Server.MapPath(filePath); tagetPhysicalPath = (physicalPath.EndsWith("\\") ? physicalPath : physicalPath + "\\"); } else { filePath += "/" + dayFolderName; } if (!Directory.Exists(tagetPhysicalPath)) { Directory.CreateDirectory(tagetPhysicalPath); } foreach (HttpPostedFile file in fileList) { var newFileName = Guid.NewGuid().ToString() + "." + Path.GetExtension(file.FileName); var webFileName = filePath + "/" + newFileName; var physicalFileName = tagetPhysicalPath + newFileName; file.SaveAs(physicalFileName); result.Add(urlHelper.Content(webFileName)); } return result; } /// /// 文件上传并解压 /// savePath是相对与web上传路径的子路径 /// 如:web上传路径为~/Content/DownFile,savePath为SOC/2018-201901,则实际上传的路径为~/Content/DownFile/SOC/2018-201901 /// /// /// /// public static IList UploadFileAndExtractFile(HttpPostedFileBase file, string savePath = null) { var physicalFileName = UploadFileInternal(file, savePath); if (physicalFileName == null) return new List(); var targetPhysicalFileName = physicalFileName.Replace(Path.GetExtension(physicalFileName), ""); var result = ZIPHelper.ExtractZFile(physicalFileName, targetPhysicalFileName); //var filePath = Const.LOCAL_SETTING_UPLOAD_FILEPATH; UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext); result = result.Select(s => urlHelper.Content("~/" + s.Replace(HttpContext.Current.Server.MapPath("~/"), "").Replace("\\", "//"))).ToList(); return result; } /// /// 文件上传并解压 /// savePath是相对与web上传路径的子路径 /// 如:web上传路径为~/Content/DownFile,savePath为SOC/2018-201901,则实际上传的路径为~/Content/DownFile/SOC/2018-201901 /// /// /// /// /// /// public static IList UploadFileAndExtractFile(HttpPostedFileBase file, out string fileSourceWebPath, out string errorSourceWebPath, string savePath = null) { var physicalFileName = UploadFileInternal(file, out fileSourceWebPath, out errorSourceWebPath, savePath); if (physicalFileName == null) return new List(); var targetPhysicalFileName = physicalFileName.Replace(Path.GetExtension(physicalFileName), ""); var result = ZIPHelper.ExtractZFile(physicalFileName, targetPhysicalFileName); //var filePath = Const.LOCAL_SETTING_UPLOAD_FILEPATH; UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext); result = result.Select(s => urlHelper.Content("~/" + s.Replace(HttpContext.Current.Server.MapPath("~/"), "").Replace("\\", "//"))).ToList(); return result; } /// /// 删除File /// /// public static void DeleteFile(string filePath) { DeleteFileInternal(filePath); } /// /// 批量下载文件并打包 /// /// 打包文件的文件名 /// 下载的临时文件的虚拟路径数组 /// 远程文件地址数组 /// fileNames和remotePaths的长度必须一致,并且同一索引的fileNames元素和remotePaths元素对应 /// /// fileInfos[0].FileName = "文件1.txt"; /// fileInfos[0].RemotePath = "http://www.google.com/html1.txt"; /// fileInfos[1].FileName = "文件2.txt"; /// fileInfos[1].RemotePath = "http://www.google.com/html2.txt"; /// fileInfos[2].FileName = "文件3.txt"; /// fileInfos[2].RemotePath = "http://www.google.com/html3.txt"; /// /// packagePath = "c:\aa.rar"; /// /// 执行结果:将http://www.google.com/html1.txt下载并保存为文件1.txt, /// http://www.google.com/html2.txt下载并保存为文件2.txt, /// http://www.google.com/html3.txt下载并保存为文件3.txt /// 然后将这三个文件打成压缩包,保存在c:\aa.rar中 /// public static Stream DownLoadFiles(string packageName, string downloadName, IList fileInfos) { string rarTmpPathName = "RarTmp" + Guid.NewGuid().ToString(); if (fileInfos.Count == 0) { throw new Exception("导出照片失败,找不到照片。"); //return; } #region 下载文件 string physicalPath = HttpContext.Current.Server.MapPath(Const.LOCAL_SETTING_UPLOAD_FILEPATH + "/" + rarTmpPathName); Directory.CreateDirectory(HttpContext.Current.Server.MapPath(Const.LOCAL_SETTING_UPLOAD_FILEPATH + "/" + rarTmpPathName)); //去重复 var fileInfosDistinct = fileInfos.GroupBy(x => new { x.FileName, x.RemotePath }).Select(w => new { w.Key.FileName, w.Key.RemotePath }); var downloadedFileInfo = fileInfosDistinct.Take(0).ToList(); foreach (var fileInfo in fileInfosDistinct) { try { var localfilePath = Path.GetDirectoryName(HttpContext.Current.Server.MapPath(Const.LOCAL_SETTING_UPLOAD_FILEPATH + "/" + rarTmpPathName + "/" + fileInfo.FileName)); if (!Directory.Exists(localfilePath)) { Directory.CreateDirectory(localfilePath); } FileManager.DownloadFile(Const.LOCAL_SETTING_UPLOAD_FILEPATH + "/" + rarTmpPathName + "/" + fileInfo.FileName, fileInfo.RemotePath); downloadedFileInfo.Add(fileInfo); } catch (Exception ex) { string e = ex.ToString(); continue; } } if (downloadedFileInfo.Count == 0) { throw new Exception("所有的下载文件都不存在,无法下载。"); } #endregion System.Web.HttpServerUtility server = System.Web.HttpContext.Current.Server; #region 打包 string packageFullPath = HttpContext.Current.Server.MapPath(Const.LOCAL_SETTING_UPLOAD_FILEPATH + "/" + packageName); string tmpFilePath = HttpContext.Current.Server.MapPath(Const.LOCAL_SETTING_UPLOAD_FILEPATH + "/" + rarTmpPathName); using (ZipFile zipNew = new ZipFile()) { zipNew.AlternateEncoding = Encoding.UTF8; zipNew.AlternateEncodingUsage = ZipOption.AsNecessary; //去重复,压缩时若有重复文件会报“已添加了具有相同键的项”错误 foreach (var fileInfo in downloadedFileInfo) { string UrlList = tmpFilePath + @"\" + fileInfo.FileName; zipNew.AddFile(UrlList, @"\" + Path.GetDirectoryName(fileInfo.FileName)); } zipNew.Save(packageFullPath + ".zip"); } Directory.Delete(HttpContext.Current.Server.MapPath(Const.LOCAL_SETTING_UPLOAD_FILEPATH + "/" + rarTmpPathName), true); #endregion #region 弹出下载窗口 System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; response.Clear(); response.WriteFile(HttpContext.Current.Server.MapPath(Const.LOCAL_SETTING_UPLOAD_FILEPATH + "/" + packageName + ".zip")); string httpHeader = "attachment;filename=" + server.UrlEncode(downloadName) + ".zip"; response.AppendHeader("Content-Disposition", httpHeader); response.Flush(); System.IO.File.Delete(HttpContext.Current.Server.MapPath(Const.LOCAL_SETTING_UPLOAD_FILEPATH + "/" + packageName + ".zip"));//删除临时文件 //response.End(); return response.OutputStream; #endregion } } }