2024-05-28 14:36:38 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using CsharpHttpHelper;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using DeviceRepair.DataAccess;
|
|
|
|
|
|
|
|
|
|
namespace Test
|
|
|
|
|
{
|
|
|
|
|
public partial class Form1 : Form
|
|
|
|
|
{
|
|
|
|
|
private readonly HttpClient _httpClient;
|
|
|
|
|
public static readonly string ServiceUrl = $"http://localhost/DeviceRepairAPI/";
|
|
|
|
|
string auth = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJBdXRvSUQiOjEsIkxvZ2luQ29kZSI6ImFkbWluIiwiUGFzc1dvcmQiOiIyOTAyRUExQzFDQzJDNzcwQTI5RTA4RkJGRTQyMkUwQSIsIlJlYWxOYW1lIjoi566h55CG5ZGYIiwiR2VuZGVyIjowLCJCaXJ0aGRheSI6IlwvRGF0ZSgtNjg0NzgzMzYwMDAwMClcLyIsIlBob25lIjoiMTM4MDAwMDAwMDAiLCJFbWFpbCI6ImZlbmcuY2FpQHRlY2hzY2FuLmNuIiwiUm9sZUdyb3VwIjoxLCJSb2xlTmFtZSI6bnVsbCwiU3RhdHVzIjp0cnVlLCJDcmVhdGVCeSI6MCwiQ3JlYXRlRGF0ZSI6IlwvRGF0ZSgtNjg0NzgzMzYwMDAwMClcLyIsIk1vZGlmeUJ5IjoxLCJNb2RpZnlEYXRlIjoiXC9EYXRlKDE3MDg4Nzg0NDk2NjMpXC8iLCJMYXN0TG9naW5UaW1lIjoiXC9EYXRlKC02ODQ3ODMzNjAwMDAwKVwvIiwiTGFzdFB3ZEFsdGVyVGltZSI6IlwvRGF0ZSgtNjg0NzgzMzYwMDAwMClcLyIsIkRlc2NyaXB0aW9uIjoiMTIzNDU2IiwiTWFjQWRkcmVzcyI6IjM4OkQ1OjdBOkU1OjQyOjBEIiwiSVBBZGRyZXNzIjoiMTkyLjE2OC4xMC43IiwiQ29tcHV0ZXJOYW1lIjoiSE9OT1IifQ.xvD0TiTe7L4-IqYpRRTTAOWHNo2XYzeoIAUAf_EXP8c";
|
|
|
|
|
|
|
|
|
|
public Form1()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
_httpClient = new HttpClient();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HttpHelper helper;
|
|
|
|
|
private HttpHelper web
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (helper == null)
|
|
|
|
|
helper = new HttpHelper();
|
|
|
|
|
return helper;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void button1_Click_1(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Form1_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
this.textBox1.Text = @"http://localhost/DeviceRepairAPI/api/MaintenanceForm/DownloadFile?AutoID=1";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Init(ref HttpItem item)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(ServiceUrl))
|
|
|
|
|
throw new Exception("调用API失败,参数ServiceUrl不能为null!");
|
|
|
|
|
|
|
|
|
|
item.Header.Add("auth", auth);
|
|
|
|
|
//item.Header.Add("ClientName", ComputerHelper.GetComputerName);
|
|
|
|
|
//item.Header.Add("ClientMac", ComputerHelper.GetMacAddress);
|
|
|
|
|
//item.Header.Add("IPAddress", ComputerHelper.GetIPAddress);
|
|
|
|
|
|
|
|
|
|
item.Timeout = 10000;
|
|
|
|
|
item.PostEncoding = Encoding.UTF8;
|
|
|
|
|
item.URL = JoinUrl(item.URL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// URL 拼接
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="relativePath"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private string JoinUrl(string relativePath)
|
|
|
|
|
{
|
|
|
|
|
relativePath = relativePath.TrimStart('/');
|
|
|
|
|
|
|
|
|
|
// 使用 UriKind.Absolute 表示 baseUrl 是绝对路径
|
|
|
|
|
var baseUri = new Uri(ServiceUrl, UriKind.Absolute);
|
|
|
|
|
|
|
|
|
|
var relativeUri = new Uri(baseUri, relativePath);
|
|
|
|
|
return relativeUri.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void button1_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-07-08 02:44:57 +00:00
|
|
|
|
using (FileStream fileStream = new FileStream("C:\\aaa.xlsx", FileMode.Open, FileAccess.Read))
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2024-05-28 14:36:38 +00:00
|
|
|
|
|
|
|
|
|
//DriveInfomationModel entity = DriveInformationMaintenance.Instance().GetModelByEquipmentID(id);
|
|
|
|
|
//textBox2.Text = JsonConvert.SerializeObject(entity);
|
|
|
|
|
////通过点检表主键ID获取点检表并保存到缓存目录
|
|
|
|
|
//string templatePath = MaintenanceFormVersionMaintenance.Instance.GetFileSingle(entity.MaintenanceFormVersion);
|
|
|
|
|
|
|
|
|
|
//MessageBox.Show(templatePath);
|
|
|
|
|
|
|
|
|
|
//HttpHelper http = new HttpHelper();
|
|
|
|
|
//HttpItem item = new HttpItem
|
|
|
|
|
//{
|
|
|
|
|
// URL = ServiceRoute.GetCurrentDateTime,
|
|
|
|
|
// Method = "Post",
|
|
|
|
|
// ContentType = "application/text; charset=utf-8",
|
|
|
|
|
//};
|
|
|
|
|
//Init(ref item);
|
|
|
|
|
|
|
|
|
|
////请求的返回值对象
|
|
|
|
|
//HttpResult result = http.GetHtml(item);
|
|
|
|
|
////获取返回值
|
|
|
|
|
//string CurrentDateTime = result.Html.Trim('"');
|
|
|
|
|
//textBox1.Text = CurrentDateTime;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//string a = "2024.01";
|
|
|
|
|
//DateTime t;
|
|
|
|
|
//if (DateTime.TryParse(a, out t))
|
|
|
|
|
//{
|
|
|
|
|
// MessageBox.Show(t.ToString("yyyy-MM-dd"));
|
|
|
|
|
//}
|
|
|
|
|
//else
|
|
|
|
|
//{
|
|
|
|
|
// MessageBox.Show("失败");
|
|
|
|
|
//}
|
|
|
|
|
//return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//using (var db = SqlSugarHelper.dbClient)
|
|
|
|
|
//{
|
|
|
|
|
//int Year = DateTime.Today.Year;
|
|
|
|
|
//int Month = DateTime.Today.Month;
|
|
|
|
|
//var lst = db.Queryable<DrivePlanModel>()
|
|
|
|
|
// .Where(x => !SqlFunc.Subqueryable<MaintenanceRecordModel>().Where(p => p.PlanPrimaryID == x.AutoID && p.PlanType == x.MaintenanceType).Any())
|
|
|
|
|
// .Where(x => x.MaintenanceYear == Year && x.MaintenanceMonth == Month).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// var datas = db.Queryable<DrivePlanModel, DriveInfomationModel, MaintenanceFormVersionModel>((p, device, form) => new object[]
|
|
|
|
|
// {
|
|
|
|
|
// JoinType.Left,p.EquipmentID == device.AutoID,
|
|
|
|
|
// JoinType.Left,device.MaintenanceFormVersion == form.AutoID
|
|
|
|
|
// }).Where((p, device, form) => p.AutoID == 362).Select((p, device, form) => new { p, form }).First();
|
|
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////创建Httphelper对象
|
|
|
|
|
//HttpHelper http = new HttpHelper();
|
|
|
|
|
////创建Httphelper参数对象
|
|
|
|
|
//HttpItem item = new HttpItem()
|
|
|
|
|
//{
|
|
|
|
|
// URL = textBox1.Text,
|
|
|
|
|
// Method = "get",
|
|
|
|
|
// ContentType = "application/vnd.ms-excel",
|
|
|
|
|
// ResultType = ResultType.Byte
|
|
|
|
|
//};
|
|
|
|
|
//HttpResult result = http.GetHtml(item);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////Header
|
|
|
|
|
//WebHeaderCollection header = result.Header;
|
|
|
|
|
//if (header != null)
|
|
|
|
|
//{
|
|
|
|
|
// string fileName = header["FileName"];
|
|
|
|
|
// if (!string.IsNullOrWhiteSpace(fileName))
|
|
|
|
|
// {
|
|
|
|
|
// File.WriteAllBytes($@"C:\Users\Clove\Desktop\{fileName}", result.ResultByte);
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//upload();
|
|
|
|
|
|
|
|
|
|
//UploadFileAsync(@"D:\WorkStation\设备维护管理软件\Documentation\FM-P0047.02西铁城纵切机床定期维护点检表 - 副本.xlsx");
|
|
|
|
|
//string filePath = @"D:\WorkStation\设备维护管理软件\Documentation\FM-P0047.02西铁城纵切机床定期维护点检表 - 副本.xlsx";
|
|
|
|
|
//using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
|
|
|
|
|
//using (BinaryReader br = new BinaryReader(fileStream))
|
|
|
|
|
//{
|
|
|
|
|
// long numBytes = new FileInfo(filePath).Length;
|
|
|
|
|
// byte[] fileBytes = br.ReadBytes((int)numBytes);
|
|
|
|
|
// HttpResult result = web.GetHtml(new HttpItem
|
|
|
|
|
// {
|
|
|
|
|
// URL = textBox1.Text,
|
|
|
|
|
// Method = "post",
|
|
|
|
|
// //Postdata = JsonConvert.SerializeObject(new { value = "fsdjlk" }),
|
|
|
|
|
// ContentType = "text/json",
|
|
|
|
|
// ResultType = ResultType.Byte,
|
|
|
|
|
// PostdataByte = fileBytes,
|
|
|
|
|
// Accept = "application/octet-stream",
|
|
|
|
|
// Header =
|
|
|
|
|
// {
|
|
|
|
|
// { "auth","eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJBdXRvSUQiOjEsIkxvZ2luQ29kZSI6ImFkbWluIiwiUGFzc1dvcmQiOiI5NkU3OTIxODk2NUVCNzJDOTJBNTQ5REQ1QTMzMDExMiIsIlJlYWxOYW1lIjoi566h55CG5ZGYIiwiR2VuZGVyIjoxLCJCaXJ0aGRheSI6IlwvRGF0ZSgxNjkzNDcxNjA0NzcwKVwvIiwiUGhvbmUiOiIxMzgwMDAwMDAwMCIsIlJ1bGVHcm91cCI6MCwiTWFjQWRkcmVzcyI6IjM4OkQ1OjdBOkU1OjQyOjBEIiwiSVBBZGRyZXNzIjoiMTkyLjE2OC4xMC43IiwiQ29tcHV0ZXJOYW1lIjoiSE9OT1IifQ.oaDvAu6wcWhSGottHf1yArxhqM2SAAsUYPmhhNvBdeY" }
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
// MessageBox.Show(result.Html);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private const int ChunkSize = 1024; // 1MB
|
|
|
|
|
private long _totalSize;
|
|
|
|
|
private long _uploadedSize;
|
|
|
|
|
string _filePath;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async void upload()
|
|
|
|
|
{
|
|
|
|
|
_filePath = @"D:\WorkStation\设备维护管理软件\Documentation\FM-P0047.02西铁城纵切机床定期维护点检表 - 副本.xlsx";
|
|
|
|
|
_totalSize = new FileInfo(_filePath).Length;
|
|
|
|
|
_uploadedSize = 0;
|
|
|
|
|
progressBar1.Minimum = 0;
|
|
|
|
|
progressBar1.Maximum = (int)(_totalSize / ChunkSize);
|
|
|
|
|
progressBar1.Value = 0;
|
|
|
|
|
|
|
|
|
|
await UploadChunks();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task UploadChunks()
|
|
|
|
|
{
|
|
|
|
|
using (FileStream fileStream = new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
|
|
|
|
|
{
|
|
|
|
|
byte[] buffer = new byte[ChunkSize];
|
|
|
|
|
int bytesRead;
|
|
|
|
|
long startByte = 0;
|
|
|
|
|
|
|
|
|
|
while ((bytesRead = await fileStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
|
|
|
|
|
{
|
|
|
|
|
HttpContent content = new ByteArrayContent(buffer, 0, bytesRead);
|
|
|
|
|
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("multipart/form-data");
|
|
|
|
|
content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
|
|
|
|
|
{
|
|
|
|
|
FileName = Path.GetFileName(_filePath),
|
|
|
|
|
Size = bytesRead,
|
|
|
|
|
};
|
|
|
|
|
content.Headers.ContentRange = new System.Net.Http.Headers.ContentRangeHeaderValue(startByte, startByte + bytesRead - 1, _totalSize);
|
|
|
|
|
|
|
|
|
|
HttpResponseMessage response = await _httpClient.PostAsync(textBox1.Text, content);
|
|
|
|
|
|
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
_uploadedSize += bytesRead;
|
|
|
|
|
progressBar1.Value = (int)(_uploadedSize / ChunkSize);
|
|
|
|
|
startByte += bytesRead;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Handle error, such as logging the error and possibly retrying the upload
|
|
|
|
|
MessageBox.Show("Error uploading chunk. Please try again.");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task UploadFileAsync(string filePath)
|
|
|
|
|
{
|
|
|
|
|
using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
|
|
|
|
|
using (var multipartContent = new MultipartFormDataContent())
|
|
|
|
|
{
|
|
|
|
|
var fileContent = new StreamContent(fileStream);
|
|
|
|
|
fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("multipart/form-data");
|
|
|
|
|
fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
|
|
|
|
|
{
|
|
|
|
|
FileName = Guid.NewGuid() + "",
|
|
|
|
|
Size = fileStream.Length
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
multipartContent.Add(fileContent, "file", Path.GetFileName(filePath));
|
|
|
|
|
|
|
|
|
|
HttpResponseMessage response = await _httpClient.PostAsync(textBox1.Text, multipartContent);
|
|
|
|
|
|
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("File uploaded successfully.");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Failed to upload file. Status code: " + response.StatusCode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void button2_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
// PlanAccess.Instance.Single(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|