using BreakpointTransmission.Client; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Web; using Transmission.SDK; namespace TransmissionClient { public class DataFileUpload { /// /// 压缩文件存储文件夹 /// private readonly string ZipFileDirectory = "Upload"; /// /// 文件基础目录 /// private string BaseDirectory { get; set; } /// /// 文件上传 /// private BreakpointUploader DataBreakpointUploader { get; set; } /// /// 数据压缩包文件上传实现类 /// ///站点名称 public DataFileUpload(string fileName) { if (string.IsNullOrWhiteSpace(fileName)) { fileName = "PosDataUpload"; } this.BaseDirectory = IISWorkerHelper.GetPhysicPath(fileName); DataBreakpointUploader = new BreakpointUploader(1024 * 1024 * 1, UploadType.Append); DataBreakpointUploader.OnUploading += Uploader_OnUploading; } /// /// 文件传输进度处理事件 /// /// /// private void Uploader_OnUploading(object sender, UploadEventArgs e) { } /// /// 文件上传 /// /// 上传地址 /// public bool UploadDataZipFile(string uploadURL) { try { //压缩文件保存的路径 string str_ZipFileDir = Path.Combine(BaseDirectory, ZipFileDirectory); if (!Directory.Exists(str_ZipFileDir)) { //压缩文件不存在,直接结束 return false; } //读取压缩文件存储目录下全部压缩文件 FileInfo[] file_DataFileList = new DirectoryInfo(str_ZipFileDir).GetFiles("*.zip", SearchOption.AllDirectories); foreach (FileInfo file_DataFile in file_DataFileList) { //判断压缩文件是否已上传,未上传的有效压缩文件才进行上传 if (file_DataFile.Length > 0) { Internal.Header.M_Headers = new KeyValuePair("x-file-type", HttpUtility.UrlEncode(file_DataFile.Directory.Name)); UploadStatus status = DataBreakpointUploader.Upload(file_DataFile.FullName, uploadURL); if (status == UploadStatus.Completed) { //等待1秒,用于释放被传输占用的文件资源 System.Threading.Thread.Sleep(1000); File.Move(file_DataFile.FullName, Path.Combine(BaseDirectory, "Zip", file_DataFile.Name)); } } } return true; } catch (Exception ex) { LogHelper.WriteServiceLog("数据文件压缩包上传失败:" + ex.Message); return false; } } } }