【发布时间】:2021-12-14 15:43:30
【问题描述】:
所以我正在尝试将一个大文件上传到电子邮件以使用 MS Graph 发送,但在我的解决方案中,最后一个卡盘总是因 HTTP400 而失败
这是一个 SPFx WebPart,所以也许我缺少一些 SharePoint 的东西?
{"error":{"code":"ErrorMessageSizeExceeded","message":"The message exceeds the maximum supported size."}}
有:
- 创建草稿消息
- 创建上传会话
- 分块上传文件
- 发送草稿消息
这是我的代码:
//creates the drarf message
const draftMessageResponse = await GraphService._client.api('me/messages').post(emailPostBody.message);
const draftMessageId:string = draftMessageResponse.id;
for(let i = 0; i < largeFileUploads.length; i++) {
const attachment:IEmailAttachment = largeFileUploads[i];
const buffer:ArrayBuffer = await getArrayBufferFromBase64(attachment.content);
//creates the upload instance
const uploadSession = {
AttachmentItem: {
attachmentType: 'file',
name: attachment.filename,
size: buffer.byteLength
}
};
const uploadSessionRes = await GraphService._client.api(`/me/messages/${draftMessageId}/attachments/createUploadSession`).post(uploadSession);
const uploadEndpoint:string = uploadSessionRes.uploadUrl;
//uploads the file
let uploadIndex:number = 0;
while(true) {
//calculates the end index of the slice
let endIndex = uploadIndex + BYTES_2MB - 1;
//gets the slice
let slice:ArrayBuffer;
if(endIndex >= buffer.byteLength) {
endIndex = buffer.byteLength - 1;
slice = buffer.slice(uploadIndex);
} else {
slice = buffer.slice(uploadIndex, endIndex + 1);
}
const httpOptions:IHttpClientOptions = {
method: 'PUT',
headers: [
['Content-Length', `${ slice.byteLength }`],
['Content-Range', `bytes ${uploadIndex}-${endIndex}/${buffer.byteLength}`],
['Content-Type', 'application/octet-stream']
],
body: slice
};
const response = await GraphService._httpClient.fetch(uploadEndpoint, HttpClient.configurations.v1, httpOptions);
if(!response.ok) {
break;
}
const json = await response.json();
console.log(json);
if(json.nextExpectedRanges) {
uploadIndex = parseFloat(json.nextExpectedRanges[0]);
} else {
//if there is no next range then break the loop
break;
}
}
}
//sends the e-mail
await GraphService._client.api(`/me/messages/${draftMessageId}/send`).post('');
GraphService._client 是使用 SPFx 上下文获取的 MS Graph Client,GraphService._httpClient 是从 SPFx 上下文获取的 HttpClient。
我只是不知道最后一个请求有什么问题。这是标题:
content-length: 1180876
content-range: bytes 39845888-41026763/41026764
content-type: application/octet-stream
【问题讨论】: