コード例 #1
0
        /// <summary>
        /// 保存上传文件临时文件夹
        /// </summary>
        /// <param name="file">上传文件</param>
        /// <param name="cfg">上传配置信息</param>
        /// <returns>新保存文件路径信息</returns>
        public static string SaveAsTemporary(HttpPostedFileBase file, UploadConfig cfg)
        {
            string ext      = Path.GetExtension(file.FileName);
            string fileName = CreatTemporaryFileName(ext, cfg);

            return(Save(file, fileName, cfg));
        }
コード例 #2
0
        /// <summary>
        /// 保存文件流
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="fileName"></param>
        /// <param name="ext"></param>
        /// <param name="cfg"></param>
        /// <returns></returns>
        public static string Save(Stream stream, string fileName, string ext, UploadConfig cfg)
        {
            Uri uri = null;

            if (string.IsNullOrWhiteSpace(fileName) || Uri.TryCreate(fileName, UriKind.Absolute, out uri))
            {
                fileName = CreateFileName(ext, cfg);
            }
            else
            {
                fileName = CreateFileName(ext, cfg, fileName);
            }
            string diskFileName = GetUploadFileDiskPath(fileName, cfg);
            string dir          = Path.GetDirectoryName(diskFileName);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            var bytes = ReadBytes(stream);

            stream.Close();
            stream.Dispose();

            File.WriteAllBytes(diskFileName, bytes);
            return(fileName);
        }
コード例 #3
0
        /// <summary>
        /// 保存上传文件
        /// </summary>
        /// <param name="file">上传文件</param>
        /// <param name="cfg">上传配置信息</param>
        /// <param name="customerFileName">自定义文件名</param>
        /// <returns>新保存文件路径信息</returns>
        public static string SaveAs(HttpPostedFileBase file, UploadConfig cfg, string customerFileName = null)
        {
            string ext      = Path.GetExtension(file.FileName);
            string fileName = !string.IsNullOrWhiteSpace(customerFileName) ? customerFileName : file.FileName;

            return(Save(file, fileName, cfg));
        }
コード例 #4
0
        /// <summary>
        /// 上传文件检查
        /// </summary>
        /// <param name="file">文件信息</param>
        /// <param name="cfg">上传配置信息</param>
        public static void Check(HttpPostedFileBase file, UploadConfig cfg)
        {
            if (file == null || file.ContentLength <= 0)
            {
                throw new Exception("上传文件不能为空!");
            }

            //获取当前请求HttpContext
            var context = ApplicationObject.CurrentOneHttpContext;

            //获取上传文件扩展名
            var ext = System.IO.Path.GetExtension(file.FileName);

            ext = string.IsNullOrWhiteSpace(ext) ? string.Empty : ext.Trim().ToUpper();

            //检查此扩展名文件是否允许上传
            if (!cfg.AllowType.Contains(ext))
            {
                throw new Exception(string.Format("不支持{0}文件类型上传", ext));
            }


            //检查文件大小是否超过限制
            if ((file.ContentLength / 1024) > cfg.MaxFileSize)
            {
                throw new Exception(string.Format("上传文件必须小于{0}", TypeUtil.FileSize(cfg.MaxFileSize * 1024)));
            }
        }
コード例 #5
0
        private static string GetUploadFileDiskPath(string url, UploadConfig cfg)
        {
            if (string.IsNullOrWhiteSpace(url))
            {
                return(string.Empty);
            }

            Uri uri = new Uri(url, UriKind.RelativeOrAbsolute);

            if (uri.IsAbsoluteUri)
            {
                url = uri.AbsolutePath.Substring(1);
            }

            var p        = System.Web.VirtualPathUtility.IsAppRelative(url) ? System.Web.VirtualPathUtility.ToAbsolute(url) : url;
            var fullPath = string.Empty;

            if (System.IO.Path.IsPathRooted(p))
            {
                fullPath = httpContext.Server.MapPath(p);
            }
            else
            {
                string basePath = string.IsNullOrWhiteSpace(cfg.BasePath) ? ApplicationObject.CurrentOneHttpContext.Server.MapPath("~/") : cfg.BasePath;
                fullPath = System.IO.Path.Combine(basePath, url);
            }
            return(fullPath);
        }
コード例 #6
0
        /// <summary>
        /// 创建一个指定类型带路径的临时文件名
        /// </summary>
        /// <param name="ext">文件类型</param>
        /// <returns></returns>
        private static string CreatTemporaryFileName(string ext, UploadConfig cfg)
        {
            string folder = WebHelper.MapPath(cfg.Folder) + Path.DirectorySeparatorChar.ToString();
            string name   = Guid.NewGuid().ToString("N");

            return(string.Concat(folder, name, ext));
        }
コード例 #7
0
        /// <summary>
        /// 创建一个指定类型带路径的临时文件名
        /// </summary>
        /// <param name="ext">文件类型</param>
        /// <returns></returns>
        private static string CreatTemporaryFileName(string ext, UploadConfig cfg)
        {
            string folder = System.Web.Mvc.UrlHelper.GenerateContentUrl(cfg.TempPath, new HttpContextWrapper(ApplicationObject.CurrentOneHttpContext));

            folder = System.Web.VirtualPathUtility.AppendTrailingSlash(folder);
            string name = Guid.NewGuid().ToString("N");

            return(string.Concat(folder, name, ext));
        }
コード例 #8
0
        /// <summary>
        /// 保存上传文件
        /// </summary>
        /// <param name="file">上传文件</param>
        /// <param name="fileName">保存文件名</param>
        /// <param name="cfg">上传配置信息</param>
        /// <returns>返回地址</returns>
        public static string Save(HttpPostedFileBase file, string fileName, UploadConfig cfg)
        {
            Check(file, cfg);

            //获取上传文件扩展名
            var ext = System.IO.Path.GetExtension(file.FileName);

            ext = string.IsNullOrWhiteSpace(ext) ? string.Empty : ext.Trim().ToUpper();

            return(Save(file.InputStream, fileName, ext, cfg));
        }
コード例 #9
0
        /// <summary>
        /// 创建一个指定类型带路径的文件名
        /// </summary>
        /// <param name="ext">文件类型</param>
        /// <param name="cfg">上传配置</param>
        /// <param name="customerFileName">自定义文件名</param>
        /// <returns></returns>
        private static string CreateFileName(string ext, UploadConfig cfg, string customerFileName = null)
        {
            string folder = System.Web.Mvc.UrlHelper.GenerateContentUrl(cfg.Folder, new HttpContextWrapper(ApplicationObject.CurrentOneHttpContext));

            folder = System.Web.VirtualPathUtility.AppendTrailingSlash(folder);
            folder = string.Concat(folder, DateTime.Now.ToString("yyyyMM", DateTimeFormatInfo.InvariantInfo));
            folder = System.Web.VirtualPathUtility.AppendTrailingSlash(folder);
            string name     = customerFileName.ObjToStr().Length > 0 ? customerFileName.ObjToStr() : Guid.NewGuid().ToString("N");
            var    fileName = name.Contains(ext) ? string.Concat(folder, name, ext) : string.Concat(folder, name);

            return(File.Exists(fileName) ? CreateFileName(ext, cfg) : fileName);
        }
コード例 #10
0
        /// <summary>
        /// 创建一个指定类型带路径的文件名
        /// </summary>
        /// <param name="ext">文件类型</param>
        /// <param name="cfg">上传配置</param>
        /// <param name="customerFileName">自定义文件名</param>
        /// <returns></returns>
        private static string CreateFileName(string ext, UploadConfig cfg, string customerFileName = null)
        {
            string folder = WebHelper.MapPath(cfg.Folder) + Path.DirectorySeparatorChar.ToString();

            folder = string.Concat(folder, DateTime.Now.ToString("yyyyMM", DateTimeFormatInfo.InvariantInfo)) + Path.DirectorySeparatorChar.ToString();
            string name     = customerFileName.ObjToStr().Length > 0 ? customerFileName.ObjToStr() : Guid.NewGuid().ToString("N");
            var    fileName = name.ToUpper().Contains(ext) ? string.Concat(folder, name) : string.Concat(folder, name, ext);

            if (File.Exists(fileName))
            {
                return(CreateFileName(ext, cfg));
            }
            return(fileName.Replace(Globals.GetWebDir(), string.Empty).Replace(Path.DirectorySeparatorChar.ToString(), "/"));
        }
コード例 #11
0
        private static string GetUploadFileDiskPath(string url, UploadConfig cfg)
        {
            if (string.IsNullOrWhiteSpace(url))
            {
                return(string.Empty);
            }

            Uri uri = new Uri(url, UriKind.RelativeOrAbsolute);

            if (uri.IsAbsoluteUri)
            {
                url = uri.AbsolutePath.Substring(1);
            }
            return(WebHelper.MapPath(url));
        }
コード例 #12
0
        /// <summary>
        /// 从XML载入上传配置
        /// </summary>
        /// <param name="cfg"></param>
        /// <param name="el"></param>
        private static void GetConfigByXmlNote(UploadConfig cfg, XElement el)
        {
            var allowType = el.Element("allowType");

            if (allowType != null && !string.IsNullOrWhiteSpace(allowType.Value))
            {
                var exts = allowType.Value.Split(new[] { ",", ";" }, StringSplitOptions.RemoveEmptyEntries);

                cfg.AllowType = exts.Select(o => System.IO.Path.GetExtension(o).ToUpper()).ToList();
            }

            var maxFileSize = el.Element("maxFileSize");

            if (maxFileSize != null && !string.IsNullOrWhiteSpace(maxFileSize.Value))
            {
                cfg.MaxFileSize = maxFileSize.Value.ObjToInt();
            }

            var tempPath = el.Element("tempPath");

            if (tempPath != null && !string.IsNullOrWhiteSpace(tempPath.Value))
            {
                cfg.TempPath = tempPath.Value;
            }

            var queueSizeLimit = el.Element("queueSizeLimit");

            if (queueSizeLimit != null && !string.IsNullOrWhiteSpace(queueSizeLimit.Value))
            {
                cfg.QueueSizeLimit = queueSizeLimit.Value.ObjToInt();
            }

            var folder = el.Element("folder");

            if (folder != null && !string.IsNullOrWhiteSpace(folder.Value))
            {
                cfg.Folder = folder.Value;
            }

            var basePath = el.Element("basePath");

            if (basePath != null && !string.IsNullOrWhiteSpace(basePath.Value))
            {
                cfg.BasePath = basePath.Value;
            }
        }
コード例 #13
0
        /// <summary>
        /// 获取上传参数
        /// </summary>
        /// <returns>上传参数</returns>
        public static UploadConfig GetUploadConfig(string configPath, string configName)
        {
            string       path = httpContext.Server.MapPath(configPath);
            var          doc  = XDocument.Load(path);
            UploadConfig cfg  = new UploadConfig();

            if (doc != null)
            {
                var def = doc.Element("root").Element("default");
                GetConfigByXmlNote(cfg, def);

                var config = doc.Element("root").Element(configName);
                if (config != null)
                {
                    GetConfigByXmlNote(cfg, config);
                }
            }
            return(cfg);
        }
コード例 #14
0
        /// <summary>
        /// 获取上传参数
        /// </summary>
        /// <returns>上传参数</returns>
        public static UploadConfig GetUploadConfig(string configName)
        {
            string       configPath = "~/Config/upload.xml";
            string       path       = WebHelper.MapPath(configPath);
            var          doc        = XDocument.Load(path);
            UploadConfig cfg        = new UploadConfig();

            if (doc != null)
            {
                var def = doc.Element("root").Element("default");
                GetConfigByXmlNote(cfg, def);
                if (!string.IsNullOrWhiteSpace(configName))
                {
                    var config = doc.Element("root").Element(configName);
                    if (config != null)
                    {
                        GetConfigByXmlNote(cfg, config);
                    }
                }
            }
            return(cfg);
        }