【问题标题】:WinJS UWP javascript - how to read a file through a file inputWinJS UWP javascript - 如何通过文件输入读取文件
【发布时间】:2019-12-20 19:03:35
【问题描述】:

在我的 HTML 中,我有这样的输入:

<input type="file" id="csv_file_input" />

我想从该输入中读取文本内容。我试过这个:

                var fileInput = $('#csv_file_input');
                var file = fileInput[0].files[0];
                Windows.Storage.FileIO.readTextAsync(file)
                        .then(function(text) {
                            console.log(text);
                        });

但我得到了JavaScript runtime error: Type mismatch

想知道如何解决这个问题

【问题讨论】:

    标签: javascript uwp winjs


    【解决方案1】:

    请在uwp中使用FileOpenPicker获取StorageFile,避免上述问题。

    var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
    openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
    openPicker.suggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.picturesLibrary;
    // Users expect to have a filtered view of their folders depending on the scenario.
    // For example, when choosing a documents folder, restrict the filetypes to documents for your application.
    openPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg",".txt"]);
    
    // Open the picker for the user to pick a file
    openPicker.pickSingleFileAsync().then(function (file) {
        if (file) {
            // Application now has read/write access to the picked file
            WinJS.log && WinJS.log("Picked photo: " + file.name, "sample", "status");
            Windows.Storage.FileIO.readTextAsync(file).done(function (fileContent) {
                WinJS.log && WinJS.log("The following text was read from '" + SdkSample.sampleFile.name + "':\n" + fileContent, "sample", "status");
            },
                function (error) {
                    if (error.number === SdkSample.E_NO_UNICODE_TRANSLATION) {
                        WinJS.log && WinJS.log("File cannot be decoded as Unicode.", "sample", "error");
                    } else {
                        WinJS.log && WinJS.log(error, "sample", "error");
                    }
                });
    
        } else {
            // The picker was dismissed with no selected file
            WinJS.log && WinJS.log("Operation cancelled.", "sample", "status");
        }
    });
    

    【讨论】:

    • 有没有办法让这个界面使用拖放功能?
    • 我不认为该链接可以达到我想要的效果。无论如何,它适用于 c#/xaml,而不适用于 javascript/html
    • 我想你可以创建一个新线程来描述你想要的,我会检查的。
    猜你喜欢
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多