上傳使用者檔案
在下一個練習中,您將延伸應用程式,以支援檔案上傳。 使用 Microsoft Graph 上傳檔案有兩個方法。 最簡單的方法是使用單一 PUT 要求搭配資源 (例如 /me/drive/root:/FolderA/FileB.txt:/content
)。 此方法有 4 MB 的限制。 較複雜的方法涉及一系列的 HTTP 要求,做為上傳工作階段。
Microsoft Graph SDK 讓複雜的方法變得簡單。
OneDriveLargeFileUploadTask
物件會處理所有詳細資料:
const uploadTask = await MicrosoftGraph.OneDriveLargeFileUploadTask.create(
graphClient, file, {
path: "/",
fileName: file.name,
rangeSize: 1024 * 1024 // must be a multiple of 320 KiB
}
);
const response = await uploadTask.upload();
檔案引數為檔案資料流程物件。 在網頁瀏覽器中,它是瀏覽器檔案輸入元素所傳回的項目。
<input type="file" onchange="fileSelected(this);" />
當使用者選取檔案時,onchange
事件處理常式可以存取選取的檔案。
function fileSelected(e) {
// Add your code here; e.files[0] contains the file stream to upload the
// 1st file selected by the user; e.files[1] if a 2nd file was uploaded etc.
}