Exemple #1
0
        internal static string BringUrl(string fileName, byte[] fileData)
        {
            string folderPath = FileExporterConfig.GetSetting().BaseFolder;

            const string TmpFileFolder = "TempFiles";

            if (folderPath == null || folderPath.Trim().Length <= 0)
            {
                folderPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, TmpFileFolder);
            }
            else
            {
                string p = Path.GetPathRoot(folderPath);
                if (p == null || p.Trim().Length <= 0) // 说明是相对路径
                {
                    folderPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, folderPath);
                }
            }
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }
            else
            {
                CleanOldFiles(folderPath);
            }
            string filePath = Path.Combine(folderPath, fileName);

            using (FileStream stream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                stream.Write(fileData, 0, fileData.Length);
            }
            string vp = FileExporterConfig.GetSetting().VirtualPath;

            if (vp == null || vp.Trim().Length <= 0)
            {
                vp = TmpFileFolder;
            }
            else
            {
                vp = vp.Trim().Trim('/', '\\');
            }
            return(vp + "/" + HttpUtility.UrlEncode(fileName));
        }
Exemple #2
0
 private static void CleanOldFiles(string folderPath)
 {
     try
     {
         TimeSpan      time      = FileExporterConfig.GetSetting().GetExpiryTime();
         DirectoryInfo directory = new DirectoryInfo(folderPath);
         FileInfo[]    files     = directory.GetFiles();
         foreach (FileInfo file in files)
         {
             if (file.LastWriteTime.Date.Add(time) < DateTime.Now)
             {
                 file.Delete();
             }
         }
     }
     catch (Exception ex)
     {
         ExceptionHelper.HandleException(ex);
     }
 }
Exemple #3
0
        public static IFileExport CreateExporter(string exporterName)
        {
            ExporterSetting setting = FileExporterConfig.GetSetting();

            if (setting != null && exporterName != null && exporterName.Trim().Length > 0)
            {
                if (setting.FileExporterList.ContainsKey(exporterName))
                {
                    string typeName = setting.FileExporterList[exporterName];
                    if (typeName != null && typeName.Length > 0)
                    {
                        Type type = Type.GetType(typeName, true);
                        return((IFileExport)Activator.CreateInstance(type));
                    }
                }
                if (setting.Default != null && setting.Default.Length > 0 && setting.FileExporterList.ContainsKey(setting.Default))
                {
                    CreateExporter(setting.Default);
                }
            }
            return(new ExcelFileExporter());
        }