Files
lserp_cs_5.0/插件库/Lskj.Attachments/Lskj.Attachments/frmUpload.cs
T
2026-07-10 15:25:05 +08:00

237 lines
8.1 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DevExpress.XtraEditors;
using System.Threading;
using System.Globalization;
namespace Lskj.FileManager
{
public partial class frmUpload : Form
{
private bool isUploading = false;
private string fileNumTip = "上传进度({0}/{1})"; // {0}:已上传数量,{1}:总数
private string webServiceUrl = @"http://localhost:10518/WebService.asmx";
private DataTable dtTable = new DataTable();
public frmUpload()
{
InitializeComponent();
}
/// <summary>
/// 初始化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmMain_Load(object sender, EventArgs e)
{
dtTable.Columns.Add("fileIndex");
dtTable.Columns.Add("fileName");
dtTable.Columns.Add("fileSize");
dtTable.Columns.Add("fileState");
}
/// <summary>
/// 选择文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnBrowse_Click(object sender, EventArgs e)
{
if (ofdFile.ShowDialog() == DialogResult.OK)
{
foreach (string item in ofdFile.FileNames)
{
DataRow newRow = dtTable.NewRow();
newRow["fileIndex"] = dtTable.Rows.Count + 1;
newRow["fileName"] = item;
newRow["fileSize"] = new FileInfo(item).Length;
newRow["fileState"] = isUploading ? "等待上传" : "未上传";
dtTable.Rows.Add(newRow);
}
this.lblNum.Text = string.Format(fileNumTip, 0, dtTable.Rows.Count);
this.dgvMain.DataSource = dtTable;
}
}
/// <summary>
/// 转换为KB
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
private string getFileSize(long length)
{
string fileSize = "0";
if (length != 0)
{
try
{
fileSize = Math.Round(Convert.ToDecimal(length / 1024), 0, MidpointRounding.AwayFromZero).ToString("n").Replace(".00", "");
}
catch (Exception ex)
{
}
}
return fileSize;
}
/// <summary>
/// 清空文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnClear_Click(object sender, EventArgs e)
{
if (!isUploading)
{
dtTable.Rows.Clear();
dgvMain.DataSource = dtTable;
}
else
{
XtraMessageBox.Show("正在上传文件中,无法清空");
}
}
/// <summary>
/// 文件上传
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnUpload_Click(object sender, EventArgs e)
{
if (!isUploading)
{
Thread mThread = new Thread(UploadFiles);
mThread.Start();
}
else
{
XtraMessageBox.Show("正在上传文件.");
}
}
/// <summary>
/// 文件上传
/// </summary>
private void UploadFiles()
{
try
{
if (dtTable.Rows.Count == 0)
{
XtraMessageBox.Show("请先浏览文件.");
return;
}
Type type = null;
try
{
type = WSHelper.InvokeWebService(webServiceUrl);
}
catch (Exception ex)
{
XtraMessageBox.Show("WebService服务调用失败." + Environment.NewLine + ex.Message);
}
isUploading = true;
// 开始上传
int rowCount = 1;
// 调整状态
foreach (DataRow item in dtTable.Rows)
{
if (item["fileState"] + "" != "成功")
item["fileState"] = "等待上传";
}
foreach (DataRow item in dtTable.Rows)
{
string fileState = item["fileState"] + "";
string fileName = item["fileName"] + "";
string fileSize = item["fileSize"] + "";
if (fileState == "成功")
continue;
if (!File.Exists(fileName))
item["fileState"] = "文件未找到";
item["fileState"] = "上传中...";
bool isSuccessed = true;
using (FileStream mStream = File.OpenRead(fileName))
{
long mFileLength = mStream.Length;
if (mFileLength <= 0)
{
item["fileState"] = "文件为空,无需上传";
isSuccessed = false;
continue;
}
int mUploadCount = Convert.ToInt32(mFileLength / 1024) + 1;
// 初始化进度条
Invoke((EventHandler)delegate
{
this.progressBar1.Maximum = mUploadCount;
this.progressBar1.Value = 0;
});
byte[] mFileContent = new byte[1024];
for (int i = 0; i < mUploadCount; i++)
{
Invoke((EventHandler)delegate
{
this.progressBar1.Value = i + 1;
});
mStream.Read(mFileContent, 0, 1024);
bool mIsAppend = i != 0;
object obj = WSHelper.InvokeMethod(type, "UploadFile", new object[] { Path.GetFileName(fileName), mFileContent, mIsAppend });
if (!string.IsNullOrEmpty(obj + ""))
{
item["fileState"] = "失败";
isSuccessed = false;
break;
}
// 刷新界面
Invoke((EventHandler)delegate
{
this.lblNum.Text = string.Format(fileNumTip, rowCount, dtTable.Rows.Count) + string.Format(" {0}/{1}", (i + 1) * 1024, mFileLength);
});
}
if (isSuccessed)
{
rowCount++;
item["fileState"] = "成功";
}
}
isUploading = false;
}
}
catch (Exception ex)
{
UploadFiles();
}
}
/// <summary>
/// 关闭窗口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (isUploading)
{
DialogResult result = XtraMessageBox.Show("正在上传文件中,退出将中断上传.", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.No)
{
e.Cancel = true;
}
}
}
}
}