码迷,mamicode.com
首页 > 其他好文 > 详细

3.diskHelper类

时间:2018-05-28 16:51:11      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:sum   sel   class   orm   文件的   turn   名称   reac   ast   

    //文件和目录操作辅助类

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

using System.Web;

using System.Data;

 

namespace rui

{

    public class diskHelper

    {

        //创建目录

        public static void createDirectory(string path)

        {

            path = HttpContext.Current.Server.MapPath(path);

            if (Directory.Exists(path) == false)

                Directory.CreateDirectory(path);

        }

 

        //删除目录

        public static void deleteDirectory(string path)

        {

            path = HttpContext.Current.Server.MapPath(path);

            Directory.Delete(path, true);

        }

 

        //删除文件

        public static void deleteFile(string path)

        {

            path = HttpContext.Current.Server.MapPath(path);

            if (File.Exists(path))

                File.Delete(path);

        }

 

        //文件是否存在

        public static bool isExist(string path)

        {

            path = HttpContext.Current.Server.MapPath(path);

            return File.Exists(path);

        }

 

        //获取目录下所有的文件(按照图片的顺序获取图片文件)------------------------------这个方法还没看!!!

        public static List<string> getFiles(string resultPath, string designCode)

        {

            using (rui.dbHelper dbHelper = rui.dbHelper.createHelper(null))

            {

                resultPath = HttpContext.Current.Server.MapPath(resultPath);

                string sql = @" SELECT CONVERT(NVARCHAR(100),orderNo)+‘_‘+pictureCode+‘.jpg‘ AS filename FROM dbo.CusDesignPic WHERE designCode=‘" + designCode + "‘ ORDER BY orderNo ";

                DataTable table = dbHelper.ExecuteReaderDataTable(sql);

                List<string> list = new List<string>();

                foreach (DataRow row in table.Rows)

                {

                    list.Add(string.Format("{0}/{1}", resultPath, row["filename"].ToString()));

                }

                return list;

            }

        }

 

        //获取客户端上传的文件名

        public static string getUploadFileName(string filename)

        {

            //去除文件路径

            int index = filename.LastIndexOf("\\");

            if (index > 0)

                filename = filename.Substring(index + 1);

            //去除浏览器不支持的#符号

            filename = filename.Replace("#", "_");

 

            return filename;

        }

 

        //上传文件

        /// <summary>

        /// 

        /// </summary>

        /// <param name="savePath">上传文件所保存的位置</param>

        /// <returns>返回上传文件的文件名称和存储路径</returns>

        public static List<string> uploadFiles(string savePath, string nameKey)

        {

            List<string> list = new List<string>();

 

            //获取附件并保存

            HttpFileCollection files = HttpContext.Current.Request.Files;

 

            for (int i = 0; i < files.Count; i++)

            {

                if (files.AllKeys[i] == nameKey)

                {

                    string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + rui.diskHelper.getUploadFileName(files[i].FileName);

                    string filepath = string.Format("{0}/{1}", savePath, filename);

                    string PhysicalPath = HttpContext.Current.Server.MapPath("~" + filepath);

                    files[0].SaveAs(PhysicalPath);

                    list.Add(filepath);

                }

            }

            return list;

        }

 

    }

}

3.diskHelper类

标签:sum   sel   class   orm   文件的   turn   名称   reac   ast   

原文地址:https://www.cnblogs.com/feihusurfer/p/9100775.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!