This commit is contained in:
Wiesław Šoltés 2022-02-08 13:42:05 +00:00 коммит произвёл GitHub
Родитель 2f629f918d
Коммит 7a93fe0048
4 изменённых файлов: 41 добавлений и 32 удалений

Просмотреть файл

@ -19,7 +19,7 @@
@code {
[Inject] private IJSRuntime? _js { get; set; }
private Action<MemoryStream, string>? _callback;
private Action<Stream, string>? _callback;
private InputFile? _inputFile;
@ -40,25 +40,45 @@
stream.Position = 0;
_callback?.Invoke(stream, file.Name);
}
/*
/*
var stream = new MemoryStream();
var file = e.File;
await e.File.OpenReadStream().CopyToAsync(stream);
stream.Position = 0;
_callback?.Invoke(stream, file.Name);
*/
*/
_callback = null;
}
public async Task ShowInputDialog(Action<MemoryStream, string> callback)
public class InputDialogOptions
{
public Action<Stream, string>? Callback { get; set; }
public string Filter { get; set; } = ".*";
public bool AllowMultiple { get; set; } = false;
public bool OpenFolder { get; set; } = false;
}
public async Task ShowInputDialog(Action<Stream, string> callback)
{
if (_js is { } && _inputFile is { })
{
Console.WriteLine("ShowInputDialog() Begin");
_callback = callback;
_filter = ".txt";
_allowMultiple = true;
_openFolder = false;
await _js.InvokeVoidAsync("ShowInputDialog", "file");
//await _js.InvokeVoidAsync("getFile");
//var stream = await _js.InvokeAsync<FileStream>("ShowOpenFilePicker");
//_callback(stream, "test");
Console.WriteLine("ShowInputDialog() End");
}
}

Просмотреть файл

@ -1,49 +1,35 @@
// https://stackoverflow.com/questions/4628544/how-to-detect-when-cancel-is-clicked-on-file-input
// https://codepen.io/matuzo/pen/KOdpmq?editors=1111
// https://caniuse.com/?search=webkitdirectory
async function ShowInputDialog(id) {
await document.querySelector('input#' + id).click();
}
// https://web.dev/file-system-access/
// https://www.thinktecture.com/en/pwa/making-of-paint-js-file-access/
// https://developer.mozilla.org/en-US/docs/Web/API/Window/showOpenFilePicker
const pickerOpts = {
async function ShowOpenFilePicker() {
const opts = {
types: [
{
description: 'Supported files',
accept: {
'image/*': ['.png', '.gif', '.jpeg', '.jpg']
}
description: 'Text file',
accept: {'text/plain': ['.txt']},
},
],
excludeAcceptAllOption: true,
multiple: false
};
// create a reference for our file handle
let fileHandle;
const fileHandles = await window.showOpenFilePicker(opts);
const file = await fileHandles[0].getFile();
async function getFile() {
// open file picker, destructure the one element returned array
[fileHandle] = await window.showOpenFilePicker(pickerOpts);
// run code with our fileHandle
return file.stream();
}
// https://developer.mozilla.org/en-US/docs/Web/API/window/showSaveFilePicker
function getNewFileHandle() {
function ShowSaveFilePicker() {
const opts = {
types: [{
description: 'Text file',
@ -53,5 +39,9 @@ function getNewFileHandle() {
return window.showSaveFilePicker(opts);
}
// https://developer.mozilla.org/en-US/docs/Web/API/window/showDirectoryPicker
// https://developer.mozilla.org/en-US/docs/Web/API/window/showDirectoryPicker
async function ShowDirectoryPicker() {
const dirHandle = await window.showDirectoryPicker();
return dirHandle;
}

Просмотреть файл

@ -11,7 +11,7 @@ namespace BlazorDemo
{
public partial class App : Application
{
public static Func<Action<MemoryStream, string>, Task>? ShowInputDialog { get; set;}
public static Func<Action<Stream, string>, Task>? ShowInputDialog { get; set;}
public override void Initialize()
{

Просмотреть файл

@ -27,13 +27,12 @@ namespace BlazorDemo.Views
}
}
private void Callback(MemoryStream stream, string name)
private void Callback(Stream stream, string name)
{
var text = this.FindControl<TextBox>("text");
if (text is { })
{
Console.WriteLine($"Callback(): {name}");
stream.Position = 0;
using var reader = new StreamReader(stream);
var str = reader.ReadToEnd();
text.Text = str;