Skip to content

Commit 51c69ec

Browse files
author
Piotr Siatka
committed
Add donwloadFileContent method to allow get selected file.
Update documentation.
1 parent 065ce2b commit 51c69ec

File tree

14 files changed

+182
-61
lines changed

14 files changed

+182
-61
lines changed

docs/documentation/docs/controls/FilePicker.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ The FilePicker component can be configured with the following properties:
6060
| buttonLabel | string | no | Specifies the label of the file picker button. |
6161
| buttonIcon | string | no | In case it is provided the file picker will be rendered as an action button. |
6262
| onSave | (filePickerResult: IFilePickerResult) => void | yes | Handler when the file has been selected and picker has been closed. |
63-
| onChange | (filePickerResult: IFilePickerResult) => void | yes | Handler when the file selection has been changed. |
63+
| onChange | (filePickerResult: IFilePickerResult) => void | no | Handler when the file selection has been changed. |
6464
| context | ApplicationCustomizerContext | WebPartContext | yes | Current context. |
6565
| accepts | string[] | no | Array of strings containing allowed files extensions. E.g. [".gif", ".jpg", ".jpeg", ".bmp", ".dib", ".tif", ".tiff", ".ico", ".png", ".jxr", ".svg"] |
6666
| required | boolean | no | Sets the label to inform that the value is required. |
@@ -79,12 +79,12 @@ interface `IFilePickerResult`
7979

8080
Provides options for carousel buttons location.
8181

82-
| Value | Description |
82+
| Value | Type | Description |
8383
| ---- | ---- |
84-
| fileName | string | yes | File namr of the result with the extension. |
85-
| fileNameWithoutExtension | string | yes | File namr of the result without the extension. |
86-
| fileAbsoluteUrl | string | yes | Absolute URL of the file. Null in case of file upload. |
87-
| file | File | yes | JS Object representing File. Will be set in case of file upload. |
84+
| fileName | string | File namr of the result with the extension. |
85+
| fileNameWithoutExtension | string | File name of the result without the extension. |
86+
| fileAbsoluteUrl | string | Absolute URL of the file. Null in case of file upload. |
87+
| downloadFileContent | () => Promise<File> | Function allows to download file content. Returns File object. |
8888

8989

9090
![](https://telemetry.sharepointpnp.com/sp-dev-fx-controls-react/filePicker/FilePicker)

src/controls/filePicker/FilePicker.types.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,25 @@ export interface IFilePickerTab {
1515
onClose: () => void;
1616
}
1717

18+
/**
19+
* Represents the result of the FilePicker.
20+
*/
1821
export interface IFilePickerResult {
22+
/**
23+
* Selected file name with extension.
24+
*/
1925
fileName: string;
26+
/**
27+
* Selected file name without extension.
28+
*/
2029
fileNameWithoutExtension: string;
30+
/**
31+
* Absolute file URL. Undefined in case of file upload.
32+
*/
2133
fileAbsoluteUrl: string;
22-
file: File;
34+
35+
/**
36+
* Downloads file picker result content.
37+
*/
38+
downloadFileContent: () => Promise<File>;
2339
}

src/controls/filePicker/IFilePickerProps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export interface IFilePickerProps {
2525
/**
2626
* Handler when file has been changed.
2727
*/
28-
onChanged: (filePickerResult: IFilePickerResult) => void;
28+
onChanged?: (filePickerResult: IFilePickerResult) => void;
2929

3030
/**
3131
* Current context.

src/controls/filePicker/LinkFilePickerTab/LinkFilePickerTab.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ export default class LinkFilePickerTab extends React.Component<ILinkFilePickerTa
6262
*/
6363
private _handleChange = (fileUrl: string) => {
6464
const filePickerResult: IFilePickerResult = fileUrl && this._isUrl(fileUrl) ? {
65-
file: null,
6665
fileAbsoluteUrl: fileUrl,
6766
fileName: GeneralHelper.getFileNameFromUrl(fileUrl),
68-
fileNameWithoutExtension: GeneralHelper.getFileNameWithoutExtension(fileUrl)
67+
fileNameWithoutExtension: GeneralHelper.getFileNameWithoutExtension(fileUrl),
68+
downloadFileContent: () => { return this.props.fileSearchService.downloadBingContent(fileUrl, GeneralHelper.getFileNameFromUrl(fileUrl)); }
6969
} : null;
7070
this.setState({
7171
filePickerResult
@@ -95,7 +95,7 @@ export default class LinkFilePickerTab extends React.Component<ILinkFilePickerTa
9595
return strings.NoExternalLinksValidationMessage;
9696
}
9797

98-
const fileExists = await this.props.fileSearchService.fetchFile(value);
98+
const fileExists = await this.props.fileSearchService.checkFileExists(value);
9999
this.setState({ isValid: fileExists });
100100

101101
const strResult = fileExists ? '' : strings.ProvidedValueIsInvalid;

src/controls/filePicker/OneDriveFilesTab/OneDriveFilesTab.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ export class OneDriveFilesTab extends React.Component<IOneDriveFilesTabProps, IO
131131
* Is called when user selects a different file
132132
*/
133133
private _handleSelectionChange = (filePickerResult: IFilePickerResult) => {
134+
if (filePickerResult) {
135+
filePickerResult.downloadFileContent = () => { return this.props.oneDriveService.downloadSPFileContent(filePickerResult.fileAbsoluteUrl, filePickerResult.fileName); };
136+
}
134137
this.setState({
135138
filePickerResult
136139
});

src/controls/filePicker/RecentFilesTab/RecentFilesTab.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@ export default class RecentFilesTab extends React.Component<IRecentFilesTabProps
102102
//Get the selected key
103103
const selectedKey: IRecentFile = selectedItems[0] as IRecentFile;
104104
const filePickerResult: IFilePickerResult = {
105-
file: null,
106105
fileAbsoluteUrl: selectedKey.fileUrl,
107106
fileName: GeneralHelper.getFileNameFromUrl(selectedKey.fileUrl),
108-
fileNameWithoutExtension: GeneralHelper.getFileNameWithoutExtension(selectedKey.fileUrl)
107+
fileNameWithoutExtension: GeneralHelper.getFileNameWithoutExtension(selectedKey.fileUrl),
108+
downloadFileContent: () => { return this.props.fileSearchService.downloadSPFileContent(selectedKey.fileUrl, GeneralHelper.getFileNameFromUrl(selectedKey.fileUrl)); }
109109
};
110+
110111
// Save the selected file
111112
this.setState({
112113
filePickerResult

src/controls/filePicker/SiteFilePickerTab/SiteFilePickerTab.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ export default class SiteFilePickerTab extends React.Component<ISiteFilePickerTa
112112
* Is called when user selects a different file
113113
*/
114114
private _handleSelectionChange = (filePickerResult: IFilePickerResult) => {
115+
if (filePickerResult) {
116+
filePickerResult.downloadFileContent = () => { return this.props.fileBrowserService.downloadSPFileContent(filePickerResult.fileAbsoluteUrl, filePickerResult.fileName); };
117+
}
118+
// this.props.fileBrowserService
115119
this.setState({
116120
filePickerResult
117121
});

src/controls/filePicker/UploadFilePickerTab/UploadFilePickerTab.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ export default class UploadFilePickerTab extends React.Component<IUploadFilePick
7575
const file: File = files[0];
7676

7777
const filePickerResult: IFilePickerResult = {
78-
file,
7978
fileAbsoluteUrl: null,
8079
fileName: file.name,
81-
fileNameWithoutExtension: GeneralHelper.getFileNameWithoutExtension(file.name)
80+
fileNameWithoutExtension: GeneralHelper.getFileNameWithoutExtension(file.name),
81+
downloadFileContent: () => { return Promise.resolve(file); }
8282
};
8383

8484
if (GeneralHelper.isImage(file.name)) {

src/controls/filePicker/WebSearchTab/WebSearchTab.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ export default class WebSearchTab extends React.Component<IWebSearchTabProps, IW
102102
// even if it breaks the page.
103103
const selectedUrl: string = selectedItem.contentUrl.replace('http://', 'https://');
104104
selectedFileResult = {
105-
file: null,
106105
fileAbsoluteUrl: selectedUrl,
107106
fileName: GeneralHelper.getFileNameFromUrl(selectedUrl),
108-
fileNameWithoutExtension: GeneralHelper.getFileNameWithoutExtension(selectedUrl)
107+
fileNameWithoutExtension: GeneralHelper.getFileNameWithoutExtension(selectedUrl),
108+
downloadFileContent: () => { return this.props.bingSearchService.downloadBingContent(selectedUrl, GeneralHelper.getFileNameFromUrl(selectedUrl)); }
109109
};
110110
}
111111

src/controls/filePicker/controls/FileBrowser/FileBrowser.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ export class FileBrowser extends React.Component<IFileBrowserProps, IFileBrowser
434434
fileAbsoluteUrl: selectedItem.absoluteUrl,
435435
fileName: GeneralHelper.getFileNameFromUrl(selectedItem.name),
436436
fileNameWithoutExtension: GeneralHelper.getFileNameWithoutExtension(selectedItem.name),
437-
file: null
437+
downloadFileContent: null
438438
};
439439
}
440440
this.props.onChange(filePickerResult);
@@ -443,6 +443,9 @@ export class FileBrowser extends React.Component<IFileBrowserProps, IFileBrowser
443443
});
444444
}
445445

446+
/**
447+
* Handles item click.
448+
*/
446449
private _handleItemInvoked = (item: IFile) => {
447450
// If a file is selected, open the library
448451
if (item.isFolder) {

0 commit comments

Comments
 (0)