2025-03-28 09:49:56 +08:00

96 lines
3.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
{
/// <summary>
/// 压缩文件存储文件夹
/// </summary>
private readonly string ZipFileDirectory = "Upload";
/// <summary>
/// 文件基础目录
/// </summary>
private string BaseDirectory { get; set; }
/// <summary>
/// 文件上传
/// </summary>
private BreakpointUploader DataBreakpointUploader { get; set; }
/// <summary>
/// 数据压缩包文件上传实现类
/// </summary>
///<param name="fileName">站点名称</param>
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;
}
/// <summary>
/// 文件传输进度处理事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Uploader_OnUploading(object sender, UploadEventArgs e)
{
}
/// <summary>
/// 文件上传
/// </summary>
/// <param name="uploadURL">上传地址</param>
/// <returns></returns>
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<string, string>("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;
}
}
}
}