Esempio n. 1
0
        /// <summary>
        /// 获取分块数据
        /// </summary>
        /// <param name="sectionIndex"></param>
        /// <returns></returns>
        public SectionData GetFileData(int sectionIndex)
        {
            if (sectionIndex < 0 || sectionIndex >= this.TotalSectionCount)
            {
                return(null);
            }
            SectionData section = new SectionData();

            section.FileName         = this.FileName;
            section.FileSize         = this.FileSize;
            section.SectionSize      = this.SectionSize;
            section.Pages            = this.TotalSectionCount;
            section.CurrentPageIndex = sectionIndex;
            section.Data             = this.GetSection(sectionIndex);
            section.MD5 = FileSectionUtils.GetMD5Hash(section.Data);
            if (this.Setting.DataType == SectionDataType.Base64)
            {
                section.Base64String = Convert.ToBase64String(section.Data);
                section.Data         = null;
            }
            return(section);
        }
Esempio n. 2
0
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="section"></param>
        /// <returns></returns>
        public SaveResult SaveSection(SectionData section)
        {
            SaveResult result = new SaveResult();

            if (section == null || (section.Data == null && string.IsNullOrEmpty(section.Base64String)))
            {
                result.Message = "没有数据!";
                return(result);
            }
            if (string.IsNullOrEmpty(this.FileName))
            {
                this.FileName = section.FileName;
            }
            string savePath = this.Dir + System.IO.Path.DirectorySeparatorChar + this.FileName;

            //已经存在
            if (System.IO.File.Exists(savePath))
            {
                if (section.CurrentPageIndex == 0)
                {
                    System.IO.File.Delete(savePath);
                }
                else
                {
                    result.Success = true;
                    result.Message = "文件已存在!";
                    return(result);
                }
            }
            //查找特殊后缀名
            savePath += ".fstp";
            if (!System.IO.File.Exists(savePath))
            {
                //不存在则说明是首次请求进行创建, 创建不一定是 CurrentPageIndex==0 的请求
                this.CreateSaveFile(savePath, section.FileSize, section.Pages);
            }
            if (string.IsNullOrEmpty(section.MD5))
            {
                result.Success = true;
                result.Message = "MD5缺失!";
                return(result);
            }
            //验证MD5
            if (section.Data != null && section.Data.Length > 0)
            {
                string MD5 = FileSectionUtils.GetMD5Hash(section.Data);
                if (!MD5.Equals(section.MD5))
                {
                    result.Success = false;
                    result.Message = "MD5错误!";
                    return(result);
                }
                else
                {
                    this.WriteSection(savePath, section);
                }
            }
            else if (!string.IsNullOrEmpty(section.Base64String))
            {
                byte[] buffer = Convert.FromBase64String(section.Base64String);
                section.Data = buffer;
                string MD5 = FileSectionUtils.GetMD5Hash(buffer);
                if (!MD5.Equals(section.MD5))
                {
                    result.Success = false;
                    result.Message = "MD5错误!";
                    return(result);
                }
                else
                {
                    this.WriteSection(savePath, section);
                }
            }
            result.Success = true;
            //检查是否全部写完
            result.FileFinished = CheckSaveOver(savePath, section);
            return(result);
        }