文件上传添加 test窗口
parent
74fa884eda
commit
4d8da75e75
|
|
@ -15,6 +15,7 @@
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.3" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.3" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.3" />
|
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.3" />
|
||||||
<PackageReference Include="Minio" Version="6.0.4" />
|
<PackageReference Include="Minio" Version="6.0.4" />
|
||||||
|
<PackageReference Include="WindowsAPICodePack-Shell" Version="1.1.1" />
|
||||||
<PackageReference Include="WPF-UI" Version="4.0.2" />
|
<PackageReference Include="WPF-UI" Version="4.0.2" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,18 @@
|
||||||
<Window x:Class="Hopytry.MainWindow"
|
<Window
|
||||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
xmlns:local="clr-namespace:Hopytry"
|
xmlns:local="clr-namespace:Hopytry"
|
||||||
|
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml" x:Class="Hopytry.MainWindow"
|
||||||
mc:Ignorable="d"
|
mc:Ignorable="d"
|
||||||
Title="MainWindow" Height="450" Width="800">
|
Title="MainWindow" Height="450" Width="800">
|
||||||
|
|
||||||
<Grid>
|
<Grid>
|
||||||
|
|
||||||
|
<ui:Button Content="Button" HorizontalAlignment="Left" Margin="270,217,0,0" VerticalAlignment="Top" Click="Button_Click"/>
|
||||||
|
<ui:ProgressRing HorizontalAlignment="Left" Margin="489,308,0,0" VerticalAlignment="Top"/>
|
||||||
|
<ProgressBar HorizontalAlignment="Left" Height="10" Margin="342,345,0,0" VerticalAlignment="Top" Width="100"/>
|
||||||
|
|
||||||
</Grid>
|
</Grid>
|
||||||
</Window>
|
</Window>
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,8 @@ using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using FileUploader.Models;
|
using FileUploader.Models;
|
||||||
using Hopetry.Services;
|
using Hopetry.Services;
|
||||||
|
using Microsoft.Win32;
|
||||||
|
using Hopetry;
|
||||||
namespace Hopytry
|
namespace Hopytry
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
@ -35,5 +37,12 @@ namespace Hopytry
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void Button_Click(object sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
test testWindow = new test();
|
||||||
|
testWindow.Show();
|
||||||
|
this.Close(); // 关闭当前 MainWindow
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
<Window
|
||||||
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
|
xmlns:dlg="clr-namespace:Microsoft.Win32;assembly=PresentationFramework"
|
||||||
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||||
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||||
|
xmlns:local="clr-namespace:Hopetry"
|
||||||
|
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml" x:Class="Hopetry.test"
|
||||||
|
mc:Ignorable="d"
|
||||||
|
Title="test" Height="450" Width="800">
|
||||||
|
<Grid>
|
||||||
|
|
||||||
|
<ui:Button Content="选择文件" HorizontalAlignment="Left" Margin="111,197,0,0" VerticalAlignment="Top" Click="Button_Click"/>
|
||||||
|
<ProgressBar Name="progressBar" HorizontalAlignment="Left" Height="10" Margin="177,277,0,0" VerticalAlignment="Top" Width="100" ValueChanged="progressBar_ValueChanged"/>
|
||||||
|
<TextBox Name="StatusText" HorizontalAlignment="Left" Margin="385,328,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
|
||||||
|
|
||||||
|
</Grid>
|
||||||
|
</Window>
|
||||||
|
|
@ -0,0 +1,199 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Windows;
|
||||||
|
using System.Windows.Controls;
|
||||||
|
using System.Windows.Data;
|
||||||
|
using System.Windows.Documents;
|
||||||
|
using System.Windows.Input;
|
||||||
|
using System.Windows.Media;
|
||||||
|
using System.Windows.Media.Imaging;
|
||||||
|
using System.Windows.Shapes;
|
||||||
|
using Microsoft.Win32;
|
||||||
|
using Minio;
|
||||||
|
using Minio.DataModel;
|
||||||
|
using Minio.DataModel.Args;
|
||||||
|
using Minio.DataModel.Encryption;
|
||||||
|
using Microsoft.WindowsAPICodePack.Dialogs;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace Hopetry
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// test.xaml 的交互逻辑
|
||||||
|
/// </summary>
|
||||||
|
public partial class test : Window
|
||||||
|
{
|
||||||
|
private IMinioClient client;
|
||||||
|
public test()
|
||||||
|
{
|
||||||
|
InitializeComponent();
|
||||||
|
// 配置 MinIO 客户端
|
||||||
|
client = new Minio.MinioClient()
|
||||||
|
.WithEndpoint("123.132.248.154:9107") // MinIO 服务器地址
|
||||||
|
.WithCredentials("KcJPKzOsKfVq20EA4Lyh", "HY7K5EkYpRUphEdINZmAq34tsZD3PMLFTnL85y4N") // 访问密钥和密钥
|
||||||
|
.Build();
|
||||||
|
}
|
||||||
|
|
||||||
|
#region 多文件上传
|
||||||
|
|
||||||
|
private void Button_Click1(object sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
var openFileDialog = new Microsoft.Win32.OpenFileDialog();
|
||||||
|
|
||||||
|
openFileDialog.Multiselect = true; // 可选择多个文件
|
||||||
|
openFileDialog.Filter = "All Files (*.*)|*.*";
|
||||||
|
//openFileDialog.Multiselect = false;
|
||||||
|
|
||||||
|
if (openFileDialog.ShowDialog() == true)
|
||||||
|
{
|
||||||
|
foreach (string filePath in openFileDialog.FileNames)
|
||||||
|
{
|
||||||
|
// 处理选择的文件
|
||||||
|
UploadFileToMinIOWithProgress(filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private async void UploadFileToMinIOWithProgress(string filePath)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string bucketName = "test";
|
||||||
|
string objectName = System.IO.Path.GetFileName(filePath);
|
||||||
|
|
||||||
|
//判断桶是否存在
|
||||||
|
var beArgs = new BucketExistsArgs().WithBucket(bucketName);
|
||||||
|
bool found = await client.BucketExistsAsync(beArgs).ConfigureAwait(false);
|
||||||
|
if (!found)
|
||||||
|
{
|
||||||
|
var mbArgs = new MakeBucketArgs()
|
||||||
|
.WithBucket(bucketName);
|
||||||
|
await client.MakeBucketAsync(mbArgs).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
var progress = new Progress<ProgressReport>(progressReport =>
|
||||||
|
{
|
||||||
|
Dispatcher.Invoke(() =>
|
||||||
|
{
|
||||||
|
progressBar.Value = progressReport.Percentage;// 更新进度条的值
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
PutObjectArgs putObjectArgs = new PutObjectArgs()
|
||||||
|
.WithBucket(bucketName)
|
||||||
|
.WithObject(objectName)
|
||||||
|
.WithFileName(filePath)
|
||||||
|
//.WithContentType("application/octet-stream")
|
||||||
|
.WithProgress(progress);
|
||||||
|
|
||||||
|
// 上传文件并提供进度反馈
|
||||||
|
await client.PutObjectAsync(putObjectArgs);
|
||||||
|
MessageBox.Show("文件上传成功!");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
MessageBox.Show($"上传失败: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region 文件夹选择
|
||||||
|
|
||||||
|
private void Button_Click(object sender, RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
// 打开文件夹选择对话框
|
||||||
|
var dialog = new CommonOpenFileDialog
|
||||||
|
{
|
||||||
|
IsFolderPicker = true,
|
||||||
|
Title = "请选择上传文件"
|
||||||
|
};
|
||||||
|
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
|
||||||
|
{
|
||||||
|
string folderPath = dialog.FileName;
|
||||||
|
// 处理选择的文件
|
||||||
|
UploadFileToMinIO(folderPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async void UploadFileToMinIO(string folderPath)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string bucketName = "test";
|
||||||
|
|
||||||
|
// 检查存储桶是否存在,如果不存在则创建
|
||||||
|
var beArgs = new BucketExistsArgs().WithBucket(bucketName);
|
||||||
|
bool found = await client.BucketExistsAsync(beArgs).ConfigureAwait(false);
|
||||||
|
if (!found)
|
||||||
|
{
|
||||||
|
var mbArgs = new MakeBucketArgs()
|
||||||
|
.WithBucket(bucketName);
|
||||||
|
await client.MakeBucketAsync(mbArgs).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取文件夹中的所有文件
|
||||||
|
var files = Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories);
|
||||||
|
int totalFiles = files.Length;
|
||||||
|
int uploadedFiles = 0;
|
||||||
|
|
||||||
|
// 更新UI显示总文件数
|
||||||
|
Dispatcher.Invoke(() => StatusText.Text = $"Uploading {totalFiles} files...");
|
||||||
|
|
||||||
|
// 遍历文件并上传
|
||||||
|
foreach (var filePath in files)
|
||||||
|
{
|
||||||
|
string objectName = filePath.Substring(folderPath.Length + 1).Replace('\\', '/');
|
||||||
|
; // 保留文件夹结构 //重点注意后面的Replace
|
||||||
|
|
||||||
|
var putObjectArgs = new PutObjectArgs()
|
||||||
|
.WithBucket(bucketName)
|
||||||
|
.WithObject(objectName)
|
||||||
|
.WithFileName(filePath);
|
||||||
|
await client.PutObjectAsync(putObjectArgs);
|
||||||
|
|
||||||
|
uploadedFiles++;
|
||||||
|
int progress = (int)((double)uploadedFiles / totalFiles * 100);
|
||||||
|
|
||||||
|
// 更新UI显示上传进度
|
||||||
|
Dispatcher.Invoke(() =>
|
||||||
|
{
|
||||||
|
progressBar.Value = progress;
|
||||||
|
StatusText.Text = $"Uploaded {uploadedFiles} of {totalFiles} files...";
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 上传完成
|
||||||
|
Dispatcher.Invoke(() =>
|
||||||
|
{
|
||||||
|
StatusText.Text = "Upload completed!";
|
||||||
|
MessageBox.Show("All files uploaded successfully!");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Dispatcher.Invoke(() =>
|
||||||
|
{
|
||||||
|
StatusText.Text = "Upload failed!";
|
||||||
|
MessageBox.Show($"Error uploading files: {ex.Message}");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private void progressBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue