【问题标题】:How to properly upload a chunked (large file) attachment to a message?如何正确上传分块(大文件)附件到邮件?
【发布时间】:2021-12-14 15:43:30
【问题描述】:

所以我正在尝试将一个大文件上传到电子邮件以使用 MS Graph 发送,但在我的解决方案中,最后一个卡盘总是因 HTTP400 而失败

这是一个 SPFx WebPart,所以也许我缺少一些 SharePoint 的东西?

{"error":{"code":"ErrorMessageSizeExceeded","message":"The message exceeds the maximum supported size."}}

我知道从这里得到的步骤: https://docs.microsoft.com/en-us/graph/outlook-large-attachments?tabs=javascript#step-3-continue-uploading-byte-ranges-until-the-entire-file-has-been-uploaded

有:

  • 创建草稿消息
  • 创建上传会话
  • 分块上传文件
  • 发送草稿消息

这是我的代码:


            //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

【问题讨论】:

    标签: microsoft-graph-api spfx


    【解决方案1】:

    所以结果证明代码是正确的……主要是。我遇到的最大问题是我试图上传一个 17MB 的文件,而我们的邮箱附件限制为 10MB...希望它可以帮助遇到同样错误的任何人。

    【讨论】:

      猜你喜欢
      • 2010-09-16
      • 2017-10-14
      • 1970-01-01
      • 2016-03-19
      • 2011-08-29
      • 1970-01-01
      • 2018-01-20
      • 2019-12-31
      • 1970-01-01
      相关资源
      最近更新 更多