ファイルアクションヘルプクラス


ファイルアクションヘルプクラス:
    /// <summary>

    ///  

    /// </summary>

    public class FileManager

    {

        /// <summary>

        ///  

        /// </summary>

        private static string FileFolder = ConfigurationSettings.AppSettings["UploadFolder"];



        /// <summary>

        ///  

        /// </summary>

        private static string BaseFolder = HttpContext.Current.Server.MapPath("~/"+FileFolder);



        /// <summary>

        ///  

        /// </summary>

        private static string RootSite = ConfigurationSettings.AppSettings["RootSite"];



        /// <summary>

        ///  

        /// </summary>

        public static string FileBaseUrl = RootSite + "/" + FileFolder + "/";



        /// <summary>

        ///  

        /// </summary>

        /// <param name="file"></param>

        /// <returns> url</returns>

        public static string UploadFile(HttpPostedFileBase file)

        {

            string extension = Path.GetExtension(file.FileName);

            if(string.IsNullOrEmpty(extension))

            {

                return string.Empty;

            }

            string childFolder = extension.Substring(1);

            string folderName = Path.Combine(BaseFolder, childFolder);



            CreateFile(folderName);



            string fileName=Guid.NewGuid() + extension;

            string fileFullName = Path.Combine(folderName, fileName);



            CreateFileStream(fileFullName,file);



            return childFolder + "/" + fileName;

        }



        /// <summary>

        /// 

        /// </summary>

        /// <param name="fileFullName"></param>

        /// <param name="file"></param>

        public static void CreateFileStream(string fileFullName, HttpPostedFileBase file)

        {

            using (FileStream fs = new FileStream(fileFullName, FileMode.OpenOrCreate))

            {

                byte[] buffer = new byte[file.ContentLength];

                file.InputStream.Read(buffer, 0, file.ContentLength);

                fs.Write(buffer, 0, file.ContentLength);

                fs.Flush();

                fs.Close();

            }

        }





        /// <summary>

        ///  

        /// </summary>

        /// <param name="folderName"></param>

        public static void CreateFile(string folderName)

        {

            DirectoryInfo di = new DirectoryInfo(folderName);

            if (!di.Exists)

            {

                di.Create();

            }

        }







        /// <summary>

        ///  

        /// </summary>

        /// <param name="fileRelativePath">UploadFile </param>

        public static void DeleteFile(string fileRelativePath)

        {

            if (string.IsNullOrEmpty(fileRelativePath ))

            {

                return;

            }

            string fileName = Path.Combine(BaseFolder, fileRelativePath);

            FileInfo fi = new FileInfo(fileName);

            if (fi.Exists)

            {

                fi.Delete();

            }

        }



        /// <summary>

        ///  

        /// </summary>

        /// <param name="fileRelativePath"> </param>

        public static void DownLoadFile(string fileRelativePath)

        {

            string fileName = fileRelativePath.Substring(fileRelativePath.LastIndexOf('/'));

            string filePath = Path.Combine(BaseFolder, fileRelativePath);

            // FileInfo File , using() , fs.close() 

            if (!System.IO.File.Exists(filePath))

            {

                return;

            }

            FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate);

            int streamLength = (int)fs.Length;

            byte[] buffer = new byte[streamLength];

            fs.Read(buffer, 0, streamLength);

            HttpContext.Current.Response.ContentType = "application/octet-stream";

            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename="+fileName+";");

            ////attachment ---  

            ////inline ---  

            HttpContext.Current.Response.AddHeader("Content-Length", streamLength.ToString());

            HttpContext.Current.Response.BinaryWrite(buffer);

            fs.Close();

            HttpContext.Current.Response.Flush();

            HttpContext.Current.Response.End();

        }

    }