uno-platform - 你如何将文件交给用户?

标签 uno-platform webassembly

在#WASM/#UNO-platform 项目中,如何将文件交给用户?

在我的例子中,我在本地生成一个 PDF,必须下载它或在浏览器中显示它。

有什么线索吗?
问候, 迈克尔

最佳答案

目前还没有 API 可以直接做到这一点。但是你可以创建一个 data: anchor ( a) HTML 元素上的 url。

为此,您需要创建一些 JavaScript。方法如下:

IMPORTANT: following code will only work with very recent version of Uno.UI. Version starting with v3.0.0-dev.949+

<a> 创建一个 ContentControl标签

[HtmlElement("a")]
public partial class WasmDownload : ContentControl
{
    public static readonly DependencyProperty MimeTypeProperty = DependencyProperty.Register(
        "MimeType", typeof(string), typeof(WasmDownload), new PropertyMetadata("application/octet-stream", OnChanged));

    public string MimeType
    {
        get => (string) GetValue(MimeTypeProperty);
        set => SetValue(MimeTypeProperty, value);
    }

    public static readonly DependencyProperty FileNameProperty = DependencyProperty.Register(
        "FileName", typeof(string), typeof(WasmDownload), new PropertyMetadata("filename.bin", OnChanged));

    public string FileName
    {
        get => (string) GetValue(FileNameProperty);
        set => SetValue(FileNameProperty, value);
    }

    private Memory<byte> _content;

    public void SetContent(Memory<byte> content)
    {
        _content = content;
        Update();
    }

    private static void OnChanged(DependencyObject dependencyobject, DependencyPropertyChangedEventArgs args)
    {
        if (dependencyobject is WasmDownload wd)
        {
            wd.Update();
        }
    }

    private void Update()
    {
        if (_content.Length == 0)
        {
            this.ClearHtmlAttribute("href");
        }
        else
        {
            var base64 = Convert.ToBase64String(_content.ToArray());
            var dataUrl = $"data:{MimeType};base64,{base64}";
            this.SetHtmlAttribute("href", dataUrl);
            this.SetHtmlAttribute("download", FileName);
        }
    }
}

在您的 XAML 页面中使用它

<myControls:WasmDownload FileName="test.txt" x:Name="download">
    Click here to download
</myControls:WasmDownload>

请注意,与任何其他 XAML ContentControl 一样,您可以将任何内容放入控件的内容中。

在代码隐藏中设置文件内容

Loaded += (sender, e) =>
{
    download.MimeType = "text/plain";

    var bytes = Encoding.UTF8.GetBytes("this is the content");
    download.SetContent(bytes);
};

结果

Download Link

Save Dialog

File Opened

Uno 的直接支持

有一个PR #3380为所有平台原生地将此功能添加到 Uno。您也可以等待它,而不是采用自定义方式。

FileSavePicker 的 PR已合并,该功能现已在包 Uno.UI 中可用自版本3.0.0-dev.1353 .

关于uno-platform - 你如何将文件交给用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63026831/

相关文章:

go - 如何将 Go 及其 Assets 文件捆绑到 WASM 中?

xaml - 如何在 UNO UWP 中设置背景图像?

xamarin.forms - 是否可以使用 Uno Platform 将 AdMob 集成到 Android 应用程序中

iis - 我无法使用在 IIS 上发布的 blazor webassembly 执行网站

rust - 如何在 WebAssembly 中从 Rust 返回一个字符串(或类似字符串)?

javascript - 将 WebAsembly 内存传递给 WebGL2

xaml - 如何阻止 Android ScrollViewer 将其自身滚动到其第一个按钮子项

c# - 如何在自定义 FrameworkElement 上设置指针事件样式

android - Android中如何使用 `MediaElement`播放声音?

rust - 用 Rust 处理 WebAssembly 中的闭包而不是使用忘记和泄漏内存有什么更好的方法?