【发布时间】:2020-12-16 17:55:10
【问题描述】:
我正在使用 Angular 9 使用 MS Graph 将文件上传到 OneDrive。
到目前为止,我上传了一个空文件并上传了一个包含一些虚假内容(数据类型)的文件。我想我没有添加正确的流,
我阅读了“Upload or replace the contents of a DriveItem”和另外一堆文件。它说:请求正文的内容应该是要上传的文件的二进制流。
在同一个文档的示例(更新现有文件)部分中,它说:
const stream = The contents of the file goes here.;
let res = await client.api('/me/drive/items/{item-id}/content') .put(stream);
这是没用的。
我从一个对象中获取文件,我使用
onChangeFilePicker() {
this.upload(this.filePicker.nativeElement.files)
}
这给了我一个 File 对象的数组。
然后我尝试了很多不同的方法,最后一个
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)
})
})
}
创建一个包含两个字节的文件。
你知道如何解决它吗?
【问题讨论】:
-
我过去试过这个,它奏效了:string path = "D:\\LessThan4MB.txt";字节[] 数据 = System.IO.File.ReadAllBytes(path); using (Stream stream = new MemoryStream(data)) { var item = await _client.Me.Drive.Items[FolderID] .ItemWithPath("LessThan4MB.txt") .Content .Request() .PutAsync
(stream);您可以使用内存流和 PutAsync 请求尝试这种方法,看看它是否适合您的场景。 -
我会在答案中更新它。以便您可以正确阅读代码sn-p。
-
我阅读了您的回复,谢谢。它是 C#,所以我在 TypeScript 中使用了等效的想法。但它不起作用。我不断收到内容损坏的文件。
-
@DigitalOnion 嗨。我正在尝试将文件上传到一个驱动器,但无法获取访问令牌。你能帮我看看你是怎么做到的吗?如果你能回答这个问题,我还设置了赏金。
标签: angular microsoft-graph-api onedrive