diff --git a/README.md b/README.md index 10a4b649..2a2f7008 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ See [this](./install-old.md) ## API -#### [Android only] `DocumentPicker.pickDirectory()` +#### [Android and Windows only] `DocumentPicker.pickDirectory()` Open a system directory picker. Returns a promise that resolves to (`{ uri: string }`) of the directory selected by user. @@ -66,7 +66,7 @@ If specified, the picked file is copied to `NSCachesDirectory` / `NSDocumentDire This should help if you need to work with the file(s) later on, because by default, [the picked documents are temporary files. They remain available only until your application terminates](https://developer.apple.com/documentation/uikit/uidocumentpickerdelegate/2902364-documentpicker). This may impact performance for large files, so keep this in mind if you expect users to pick particularly large files and your app does not need immediate read access. -##### [UWP only] `readContent`:`boolean` +##### [Windows only] `readContent`:`boolean` Defaults to `false`. If `readContent` is set to true the content of the picked file/files will be read and supplied in the result object. @@ -107,7 +107,7 @@ The display name of the file. _This is normally the filename of the file, but An The file size of the document. _On Android some DocumentProviders may not provide this information for a document._ -##### [UWP only] `content`: +##### [Windows only] `content`: The base64 encoded content of the picked file if the option `readContent` was set to `true`. diff --git a/index.js b/index.js index 2328684e..0a8ebde0 100644 --- a/index.js +++ b/index.js @@ -191,7 +191,7 @@ export default class DocumentPicker { } static pickDirectory() { - if (Platform.OS === 'android') { + if (Platform.OS === 'android' || Platform.OS === 'windows') { return RNDocumentPicker.pickDirectory(); } else { return Promise.resolve(null); diff --git a/windows/ReactNativeDocumentPicker/RCTDocumentPickerModule.cs b/windows/ReactNativeDocumentPicker/RCTDocumentPickerModule.cs index 9115c45d..85be0214 100644 --- a/windows/ReactNativeDocumentPicker/RCTDocumentPickerModule.cs +++ b/windows/ReactNativeDocumentPicker/RCTDocumentPickerModule.cs @@ -102,6 +102,32 @@ public async Task> Pick(JSValue options) return result; } + [ReactMethod("pickDirectory")] + public async Task PickDirectory() + { + TaskCompletionSource tcs = new TaskCompletionSource(); + + await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => + { + var openFolderPicker = new FolderPicker(); + + openFolderPicker.ViewMode = PickerViewMode.List; + openFolderPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; + openFolderPicker.FileTypeFilter.Add("*"); + + var folder = await openFolderPicker.PickSingleFolderAsync(); + JSValueObject obj = new JSValueObject + { + { "uri", folder.Path } + }; + + tcs.SetResult(obj); + }); + + var result = await tcs.Task; + return result; + } + private async Task PrepareFile(StorageFile file, bool cache, bool readContent) { string base64Content = null;