首页 > 技术文章 > Open XML的上传、下载 、删除 ......文件路径

liuwj 2015-10-08 13:59 原文

 /// <summary>
        /// Get download site, if download tempfolder not existed, create it first
        /// </summary>
        /// <param name="filePath">the template file path</param>
        /// <returns>download path</returns>
        private string GetDownloadFilePath(string filePath)
        {
            string downloadFilePath = "";
            if (!string.IsNullOrEmpty(filePath))
            {
                string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
                fileName = fileName.Substring(0, fileName.LastIndexOf(".")) + "_" + DateTime.Now.ToString("yyyyMMddHHmmssffff", System.Globalization.DateTimeFormatInfo.InvariantInfo);//add current time to the fileName
                downloadFilePath = Utility.GetAppSetting("TempDirectory_Download");
                //if download temp folder not existed, create it
                if (!Directory.Exists(downloadFilePath))
                {
                    Directory.CreateDirectory(downloadFilePath);
                }
                downloadFilePath += fileName + filePath.Substring(filePath.LastIndexOf("."));
            }
            return downloadFilePath;
        }
        /// <summary>
        /// delete the temp files which were not created by today
        /// </summary>
        /// <param name="filePath">the temp directory for download</param>
        private void DeletePreviousDayData(string filePath)
        {
            try
            {
                if (!string.IsNullOrEmpty(filePath) && filePath.LastIndexOf("_") > 0)
                {
                    string currentDay = filePath.Substring(filePath.LastIndexOf("_") + 1, 8);
                    string folderPath = Utility.GetAppSetting("TempDirectory_Download");
                    if (Directory.Exists(folderPath))
                    {
                        foreach (string entry in Directory.GetFileSystemEntries(folderPath))
                        {
                            if (File.Exists(entry) && entry.LastIndexOf("_") > 0)
                            {
                                if (entry.Substring(entry.LastIndexOf("_") + 1).Length == 23)//yyyyMMddHHmmssffff + .docm
                                {
                                    string generateDate = entry.Substring(entry.LastIndexOf("_") + 1, 8);
                                    if (generateDate != currentDay)
                                    {
                                        File.Delete(entry);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch
            {

            }
        }
/// <summary>
        /// Copy file to temp path
        /// </summary>
        /// <param name="path1">file full path</param>
        /// <param name="path2">the temp full path to be copied to</param>
        /// <returns></returns>
        private string CopyFileToTempServer(string path1, string path2)
        {
            string errMsg = "";
            try
            {
                FileInfo fi = new FileInfo(path1);
                //delete file which generated at previous day in the temp file
                DeletePreviousDayData(path2);
                FileInfo fi1 = new FileInfo(path2);
                if (fi1.Exists)
                {
                    fi1.Delete();
                }
                //copy to the temp folder
                if (fi.Exists)
                {
                    fi.CopyTo(path2);
                }



            }
            catch
            {
                errMsg = "Copy file to " + path2 + " failed. Maybe you don't have its permission, or the temp file couldnot be update f                    or its readonly, please check it!";// += ex.ToString();
            }
            return errMsg;
        }

 

推荐阅读