Skip to content

Commit 369eb34

Browse files
author
Piotr Siatka
committed
#406 Fix OneDriveService CORS issue.
1 parent ed9cb6c commit 369eb34

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

src/controls/filePicker/FilePicker.types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ export interface IFilePickerResult {
3232
*/
3333
fileAbsoluteUrl: string;
3434

35+
/**
36+
* Absolute not modified file SharePoint URL.
37+
*/
38+
spItemUrl?: string;
39+
3540
/**
3641
* Downloads file picker result content.
3742
*/

src/controls/filePicker/OneDriveFilesTab/OneDriveFilesTab.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export class OneDriveFilesTab extends React.Component<IOneDriveFilesTabProps, IO
132132
*/
133133
private _handleSelectionChange = (filePickerResult: IFilePickerResult) => {
134134
if (filePickerResult) {
135-
filePickerResult.downloadFileContent = () => { return this.props.oneDriveService.downloadSPFileContent(filePickerResult.fileAbsoluteUrl, filePickerResult.fileName); };
135+
filePickerResult.downloadFileContent = () => { return this.props.oneDriveService.downloadSPFileContent(filePickerResult.spItemUrl, filePickerResult.fileName); };
136136
}
137137
this.setState({
138138
filePickerResult

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +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+
spItemUrl: selectedItem.spItemUrl,
437438
downloadFileContent: null
438439
};
439440
}

src/services/OneDriveService.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
import { sp, RenderListDataOptions } from "@pnp/sp";
33
import { WebPartContext } from "@microsoft/sp-webpart-base";
44

5-
import { SPHttpClient, SPHttpClientResponse, ISPHttpClientOptions } from '@microsoft/sp-http';
6-
import { IGetListDataAsStreamResult, IRow } from './IOneDriveService';
7-
import { GeneralHelper } from "../Utilities";
5+
import { SPHttpClient } from '@microsoft/sp-http';
86
import { FileBrowserService } from "./FileBrowserService";
9-
import { IFile, FilesQueryResult } from "./FileBrowserService.types";
7+
import { FilesQueryResult } from "./FileBrowserService.types";
108
import { ApplicationCustomizerContext } from "@microsoft/sp-application-base";
119

1210
export class OneDriveService extends FileBrowserService {
@@ -61,9 +59,19 @@ export class OneDriveService extends FileBrowserService {
6159
*/
6260
public downloadSPFileContent = async (absoluteFileUrl: string, fileName: string): Promise<File> => {
6361
try {
64-
const fileDownloadResult = await this.context.spHttpClient.get(absoluteFileUrl, SPHttpClient.configurations.v1, {
65-
method: "GET",
66-
mode: "cors"
62+
// replace url OneDrive site URL with current web url
63+
const urlTokens = absoluteFileUrl.split("/_api/");
64+
let fileUrl = `${this.context.pageContext.web.absoluteUrl}/_api/${urlTokens[1]}?`;
65+
66+
const fileInfoResult = await this.context.spHttpClient.get(fileUrl, SPHttpClient.configurations.v1);
67+
const fileInfo = await fileInfoResult.json();
68+
const oneDrvieFileUrl = fileInfo["@content.downloadUrl"];
69+
70+
console.log(oneDrvieFileUrl);
71+
const fileDownloadResult = await this.context.httpClient.get(oneDrvieFileUrl, SPHttpClient.configurations.v1, {
72+
headers: new Headers(),
73+
method: 'GET',
74+
mode: 'cors'
6775
});
6876

6977
if (!fileDownloadResult || !fileDownloadResult.ok) {

0 commit comments

Comments
 (0)