/// <summary>
        /// 从Xml载入文件信息
        /// </summary>
        /// <param name="xml"></param>
        /// <returns></returns>
        public static UploadFileModelCollection ParseByXml(string xml)
        {
            if (string.IsNullOrWhiteSpace(xml))
            {
                return(new UploadFileModelCollection());
            }
            var el      = XElement.Parse(xml);
            var attachs = new UploadFileModelCollection();

            if (el != null)
            {
                foreach (var e in el.Elements())
                {
                    UploadFileModel attach = new UploadFileModel();
                    var             name   = e.Element("name").Value;
                    var             url    = e.Element("url").Value;
                    var             size   = e.Element("size").Value;
                    attach.FileName = name;
                    attach.FileSize = size.ObjToLong();
                    attach.FilePath = url;
                    attachs.Add(attach);
                }
            }
            return(attachs);
        }
예제 #2
0
        /// <summary>
        /// 将缓存目录中的文件保存到实际保存目录,并获取文件信息
        /// </summary>
        /// <param name="attachments">文件信息字符串</param>
        /// <param name="fileSplit">文件分隔符</param>
        /// <param name="itemSplit">文件名和文件路径分隔符</param>
        /// <param name="saveFolder">保存的文件夹</param>
        /// <returns>保存的文件Xml集合</returns>
        public static UploadFileModelCollection SaveAs(string attachments, string configName, string fileSplit, string itemSplit)
        {
            var collection = new UploadFileModelCollection();

            //将缓存中的上传文件另存
            if (!string.IsNullOrWhiteSpace(attachments))
            {
                var files = attachments.Split(new string[] { fileSplit }, StringSplitOptions.RemoveEmptyEntries).ToList();
                var cfg   = GetUploadConfig(configName);

                //如果实际上传文件个数大于配置中限制的个数,则只获取限制个数的文件
                if (cfg.QueueSizeLimit > 0 && files.Count() > cfg.QueueSizeLimit)
                {
                    files = files.Take(cfg.QueueSizeLimit).ToList();
                }

                foreach (var o in files)
                {
                    var items = o.Split(new string[] { itemSplit }, StringSplitOptions.None).ToList();
                    var file  = items[0];
                    var name  = items.Count == 2 ? items[1] : System.IO.Path.GetFileName(file);

                    file = System.Web.Mvc.UrlHelper.GenerateContentUrl(file, httpContext);

                    var tempDiskPath = string.Empty;

                    if (System.IO.Path.IsPathRooted(file))
                    {
                        tempDiskPath = httpContext.Server.MapPath(file);
                    }
                    else
                    {
                        tempDiskPath = System.IO.Path.Combine(cfg.BasePath, file);
                    }
                    if (System.IO.File.Exists(tempDiskPath))
                    {
                        string folder = System.Web.Mvc.UrlHelper.GenerateContentUrl(cfg.Folder, httpContext);
                        folder = System.Web.VirtualPathUtility.AppendTrailingSlash(folder);
                        folder = string.Concat(folder, DateTime.Now.ToString("yyyyMM", DateTimeFormatInfo.InvariantInfo));

                        //保存的磁盘目录
                        string diskFolder = string.Empty;

                        if (System.IO.Path.IsPathRooted(folder))
                        {
                            diskFolder = httpContext.Server.MapPath(folder);
                        }
                        else
                        {
                            diskFolder = System.IO.Path.Combine(cfg.BasePath, folder);
                        }

                        //保存磁盘目录检查
                        if (!System.IO.Directory.Exists(diskFolder))
                        {
                            System.IO.Directory.CreateDirectory(diskFolder);
                        }

                        string fileName    = System.IO.Path.GetFileName(tempDiskPath);
                        string newFilePath = System.IO.Path.Combine(diskFolder, fileName);

                        //将文件移到实际保存路径中
                        System.IO.File.Move(tempDiskPath, newFilePath);

                        var fileInfo = new System.IO.FileInfo(newFilePath);



                        collection.Add(new UploadFileModel()
                        {
                            FileName = name,
                            FilePath = System.IO.Path.Combine(System.Web.VirtualPathUtility.AppendTrailingSlash(folder), fileName),
                            FileSize = fileInfo.Length
                        });
                    }
                }
            }

            return(collection);
        }