Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Epic] Add support for FileOpenPicker #508

Closed
9 of 12 tasks
TopperDEL opened this issue Jan 21, 2019 · 11 comments
Closed
9 of 12 tasks

[Epic] Add support for FileOpenPicker #508

TopperDEL opened this issue Jan 21, 2019 · 11 comments
Assignees
Labels
difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. epic kind/enhancement New feature or request platform/android 🤖 Categorizes an issue or PR as relevant to the Android platform platform/ios 🍎 Categorizes an issue or PR as relevant to the iOS platform platform/macos 🍏 Categorizes an issue or PR as relevant to the macOS platform platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform project/non-ui ⚙️ Categorizes an issue or PR as relevant to winrt (non-ui) triage/most-wanted

Comments

@TopperDEL
Copy link
Contributor

TopperDEL commented Jan 21, 2019

I'm submitting a...

  • Feature request

Current behavior

The FileOpenPicker works on UWP, but not on any other platform.

Expected behavior

The FileOpenPicker works an all platforms.

Child issues

Minimal reproduction of the problem with instructions

Simply make a call to the FileOpenPicker from Windows.Storage.Pickers.

Environment

Affected platform(s):

  • iOS
  • Android
  • WebAssembly
  • Windows
  • Build tasks
@jeromelaban jeromelaban added the kind/enhancement New feature or request label Jan 21, 2019
@mfe-
Copy link

mfe- commented Apr 2, 2019

Greetings, any news on this?

@ghuntley ghuntley added platform/android 🤖 Categorizes an issue or PR as relevant to the Android platform platform/ios 🍎 Categorizes an issue or PR as relevant to the iOS platform labels May 22, 2019
@MartinZikmund MartinZikmund added the platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform label Jul 12, 2019
@ghuntley
Copy link
Member

howdy @mfe-, we would accept a pull-request that implements this, even if it was a partial implementation for a single platform. I have set aside time to pair with folks like yourself to help with contributing features to the code base. Let me know if you are keen.

@mfe-
Copy link

mfe- commented Jul 23, 2019

Hi @ghuntley I'm afraid I don't know much about mono wasm and how the Uno platform works. As I'm still interested in this topic, I'm reading into it and let you know when I'm ready.

@FrancoisM
Copy link

FrancoisM commented Jan 16, 2020

After gathering pieces on the internet, here is something that is almost working for wasm:

function openFilePicker() {
    var input = document.createElement('input');
    input.type = 'file';
    input.accept = '.jpg, .png, .jpeg, .gif, .bmp, .tif, .tiff|image/*';
    input.onchange = e => {
        var file = e.target.files[0];
        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = readerEvent => {
            var content = readerEvent.target.result; // this is the content!var content = readerEvent.target.result; // this is the content!
            var selectFile = Module.mono_bind_static_method("[MyApp.Wasm] MyApp.Shared.MyPage:SelectFile");
            selectFile (content);
        };
    };
    input.click();
}

private async void OnLogoButtonClicked(object sender, RoutedEventArgs e)
        {
            MyPage.FileSelectedEvent -= OnFileSelectedEvent;
            MyPage.FileSelectedEvent += OnFileSelectedEvent;
            WebAssemblyRuntime.InvokeJS("openFilePicker();");
        }

        public static void SelectFile(string imageAsDataUrl) => FileSelectedEvent?.Invoke(null, new FileSelectedEventHandlerArgs(imageAsDataUrl));

        private void OnFileSelectedEvent(object sender, FileSelectedEventHandlerArgs e)
        {
            MyPage.FileSelectedEvent -= OnFileSelectedEvent;
            var base64Data = Regex.Match(e.FileAsDataUrl, @"data:image/(?<type>.+?),(?<data>.+)").Groups["data"].Value;
            var binData = Convert.FromBase64String(base64Data);
            ((IMyPageViewModel) DataContext).ImageBytes= binData;
        }

        private static event FileSelectedEventHandler FileSelectedEvent;
        
        private delegate void FileSelectedEventHandler(object sender, FileSelectedEventHandlerArgs args);

        private class FileSelectedEventHandlerArgs
        {
            public string FileAsDataUrl { get; }
            public FileSelectedEventHandlerArgs(string fileAsDataUrl) => FileAsDataUrl = fileAsDataUrl;

        }

@FrancoisM
Copy link

Note: it's easier to directly set the html image src:

var image = document.getElementsByTagName("img")[0];
image.src = content;

The problem is that we need a mecanism to identify the correct image. Some tag that would be set in xaml and pass trough to html for example.

@FrancoisM
Copy link

FrancoisM commented Jan 16, 2020

Here we go 😄

function openFilePicker(htmlId) {
    console.log(htmlId);
    var input = document.createElement('input');
    input.type = 'file';
    input.accept = '.jpg, .png, .jpeg, .gif, .bmp, .tif, .tiff|image/*';
    input.onchange = e => {
        var file = e.target.files[0];
        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = readerEvent => {
            var content = readerEvent.target.result; // this is the content!
            var imageDiv = document.getElementById(htmlId);
            var image = imageDiv.getElementsByTagName('img')[0];
            image.src = content;
            var selectFile = Module.mono_bind_static_method("[MyApp.Wasm] MyApp.Shared.MyPage:SelectFile");
            selectFile (content);
        };
    };
    input.click();
}

WebAssemblyRuntime.InvokeJS($"openFilePicker({MyImage.HtmlId});");

@MartinZikmund
Copy link
Member

Created separate issue for macOS implementation #3890.

@Arlodotexe
Copy link
Contributor

Arlodotexe commented Aug 31, 2020

There is a Native File System API which is implemented in Chromium browsers, and can be enabled via a flag, though the W3C spec is still marked as a Draft. Mozilla seems to be working on the spec as well (see here).

For early adopters, it could be worth adding FileOpenPicker support via this API. When the Native File System API is finalized, should the API interfaces change, they can be updated in Uno.

edit: Looking into it further, "Original trial" means you need to request a token from Google to use the API before it's ready. The token is locked to a specific origin, has a usage limit, and the entire feature will be disabled (e.g. they outright remove the flag) if used by more than 0.5% of all Chrome page loads. So it won't be feasible to add this for everyone in WASM right now.

@carldebilly carldebilly added this to the HighPriority milestone Sep 1, 2020
@jeromelaban jeromelaban removed this from the HotFixes milestone Sep 2, 2020
@MartinZikmund MartinZikmund added epic platform/macos 🍏 Categorizes an issue or PR as relevant to the macOS platform labels Sep 10, 2020
This was referenced Sep 10, 2020
@agneszitte agneszitte added the project/non-ui ⚙️ Categorizes an issue or PR as relevant to winrt (non-ui) label Sep 21, 2020
@carldebilly carldebilly added this to the 3.4 milestone Nov 11, 2020
@carldebilly carldebilly removed this from the 3.4 milestone Nov 30, 2020
@jeromelaban jeromelaban added this to the 3.5 milestone Jan 5, 2021
@MartinZikmund MartinZikmund removed this from the 3.5 milestone Jan 19, 2021
@MartinZikmund MartinZikmund self-assigned this Feb 12, 2021
@MartinZikmund MartinZikmund added this to the 3.6 milestone Feb 12, 2021
@MartinZikmund MartinZikmund changed the title Add support for FileOpenPicker [Epic] Add support for FileOpenPicker Feb 12, 2021
@jeromelaban jeromelaban added the difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. label Feb 15, 2021
@MartinZikmund MartinZikmund modified the milestones: 3.6, 3.7 Mar 22, 2021
@MartinZikmund MartinZikmund removed this from the 3.7 milestone Apr 1, 2021
@MartinZikmund
Copy link
Member

Closing this for now, only GTK missing, which has an open issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty/tbd Categorizes an issue for which the difficulty level needs to be defined. epic kind/enhancement New feature or request platform/android 🤖 Categorizes an issue or PR as relevant to the Android platform platform/ios 🍎 Categorizes an issue or PR as relevant to the iOS platform platform/macos 🍏 Categorizes an issue or PR as relevant to the macOS platform platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform project/non-ui ⚙️ Categorizes an issue or PR as relevant to winrt (non-ui) triage/most-wanted
Projects
None yet
Development

No branches or pull requests