I am working with Angular 9 to upload a file to OneDrive using MS Graph.
I got so far as to upload an empty file and upload a file with some bogus content (the datatype). I suppose I am not adding the right stream,
I read “Upload or replace the contents of a DriveItem” and another bunch of documents. it says: The contents of the request body should be the binary stream of the file to be uploaded.
In the same document, in the Example (updating an existing file) section it says:
const stream = The contents of the file goes here.;
let res = await client.api('/me/drive/items/{item-id}/content') .put(stream);
which is useless.
I am getting the files from an object, I use
onChangeFilePicker() {
this.upload(this.filePicker.nativeElement.files)
}
which gives me an array of File objects.
Then I tried many different ways, the last one
private async uploadFile(graphClient: Client, folderItemId: string, file: File) {
file.arrayBuffer().then((buffer: ArrayBuffer) => {
let u8Buffer = new Uint8Array(buffer)
graphClient
.api(`/me/drive/items/${folderItemId}:/${file.name}:/content`)
.post(u8Buffer.values())
.then(response => {
console.log('ok')
})
.catch(error => {
console.error(error.stack)
})
})
}
which creates a file with two bytes.
Do you have any idea on how to solve it?