Excel file drag and drop

Jetzt kann man auch Excel files direkt aus dem Browser ins ENI ziehen. Diese werden im
Hintergrund heruntergeladen und in einer temporären Datei gespeichert. Damit wird der
Schritt erst die Datei herunterzuladen und vom lokalen Verzeichnis aus zu öffnen
übersprungen.
This commit is contained in:
Daniel Schick 2022-08-18 17:18:54 +02:00
parent fd8a809a0f
commit 6a5e719384

View File

@ -6,6 +6,7 @@ using bsmd.database;
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Windows;
namespace ENI2.EditControls
@ -28,61 +29,109 @@ namespace ENI2.EditControls
#region Drag&Drop event handler
private void imageSource_Drop(object sender, DragEventArgs e)
private async void imageSource_Drop(object sender, DragEventArgs e)
{
string[] files = (string[]) e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
Console.WriteLine(file);
if(files.Length > 0)
if (files != null)
{
if(File.Exists(files[0]))
foreach (string file in files)
Console.WriteLine(file);
if (files.Length > 0)
{
if(files[0].EndsWith("xls") || files[0].EndsWith("xlsx"))
if (File.Exists(files[0]))
{
_sourcePath = files[0];
textBoxSource.Text = _sourcePath;
if (files[0].EndsWith("xls") || files[0].EndsWith("xlsx"))
{
_sourcePath = files[0];
textBoxSource.Text = _sourcePath;
}
else
{
textBoxSource.Text = null;
_sourcePath = null;
}
}
else
{
textBoxSource.Text = null;
_sourcePath = null;
}
}
else
{
textBoxSource.Text = null;
_sourcePath = null;
EnableCompareButton();
}
}
else
{
string link = (string)e.Data.GetData(DataFormats.Text);
if(link != null)
{
// check if it is really an url, try to download the file and open it
if(Uri.TryCreate(link, UriKind.Absolute, out Uri uri))
{
HttpClient client = new HttpClient();
var response = await client.GetAsync(uri);
string fileName = Path.GetTempPath() + Guid.NewGuid().ToString() + ".xlsx";
using (var fs = new FileStream(fileName, FileMode.CreateNew))
{
await response.Content.CopyToAsync(fs);
textBoxSource.Text = link;
_sourcePath = fileName;
EnableCompareButton();
}
}
}
EnableCompareButton();
}
}
private void imageTarget_Drop(object sender, DragEventArgs e)
private async void imageTarget_Drop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
Console.WriteLine(file);
if (files.Length > 0)
if (files != null)
{
if (File.Exists(files[0]))
foreach (string file in files)
Console.WriteLine(file);
if (files.Length > 0)
{
if (files[0].EndsWith("xls") || files[0].EndsWith("xlsx"))
if (File.Exists(files[0]))
{
_targetPath = files[0];
textBoxTarget.Text = _targetPath;
if (files[0].EndsWith("xls") || files[0].EndsWith("xlsx"))
{
_targetPath = files[0];
textBoxTarget.Text = _targetPath;
}
else
{
_targetPath = null;
textBoxTarget.Text = null;
}
}
else
{
_targetPath = null;
textBoxTarget.Text = null;
}
EnableCompareButton();
}
else
}
else
{
string link = (string)e.Data.GetData(DataFormats.Text);
if (link != null)
{
_targetPath = null;
textBoxTarget.Text = null;
// check if it is really an url, try to download the file and open it
if (Uri.TryCreate(link, UriKind.Absolute, out Uri uri))
{
HttpClient client = new HttpClient();
var response = await client.GetAsync(uri);
string fileName = Path.GetTempPath() + Guid.NewGuid().ToString() + ".xlsx";
using (var fs = new FileStream(fileName, FileMode.CreateNew))
{
await response.Content.CopyToAsync(fs);
textBoxTarget.Text = link;
_targetPath = fileName;
EnableCompareButton();
}
}
}
EnableCompareButton();
}
}