Redigera

Dela via


attachment: createUploadSession

Namespace: microsoft.graph

Important

APIs under the /beta version in Microsoft Graph are subject to change. Use of these APIs in production applications is not supported. To determine whether an API is available in v1.0, use the Version selector.

Create an upload session that allows an app to iteratively upload ranges of a file, so as to attach the file to an Outlook item. The item can be a message or event.

Use this approach to attach a file if the file size is between 3 MB and 150 MB. To attach a file that's smaller than 3 MB, do a POST operation on the attachments navigation property of the Outlook item; see how to do this for a message or for an event.

As part of the response, this action returns an upload URL that you can use in subsequent sequential PUT queries. Request headers for each PUT operation let you specify the exact range of bytes to be uploaded. This allows transfer to be resumed, in case the network connection is dropped during upload.

The following are the steps to attach a file to an Outlook item using an upload session:

  1. Create an upload session.
  2. Within that upload session, iteratively upload ranges of bytes (up to 4 MB each time) until all the bytes of the file have been uploaded, and the file is attached to the specified item.
  3. Save the ID for the attachment for future access.
  4. Optional: Delete the upload session.

See attach large files to Outlook messages or events for an example.

Tip

Exchange Online lets administrators customize the message size limit for Microsoft 365 mailboxes, including any message attachments. By default, this message size limit is 35 MB. Find out how to customize the maximum message size to support attachments larger than the default limit for your tenant.

Important

Be aware of a known issue if you're attaching a large file to a message or event in a shared or delegated mailbox.

This API is available in the following national cloud deployments.

Global service US Government L4 US Government L5 (DOD) China operated by 21Vianet

Permissions

Choose the permission or permissions marked as least privileged for this API. Use a higher privileged permission or permissions only if your app requires it. For details about delegated and application permissions, see Permission types. To learn more about these permissions, see the permissions reference.

Permission type Least privileged permissions Higher privileged permissions
Delegated (work or school account) Calendars.ReadWrite Mail.ReadWrite
Delegated (personal Microsoft account) Calendars.ReadWrite Mail.ReadWrite
Application Calendars.ReadWrite Mail.ReadWrite

HTTP request

To create an upload session for attaching a file to an event:

POST /me/events/{id}/attachments/createUploadSession

To create an upload session for attaching a file to a message:

POST /me/messages/{id}/attachments/createUploadSession

Request headers

Name Description
Authorization Bearer {token}. Required. Learn more about authentication and authorization.

Request body

In the request body, provide a JSON object with the following parameters.

Parameter Type Description
AttachmentItem attachmentItem Represents attributes of the item to be uploaded and attached. At minimum, specify the attachment type (file), a name, and the size of the file.

Response

If successful, this method returns a 201 Created response code and a new uploadSession object in the response body.

Note:

The uploadUrl property returned as part of the uploadSession response object is an opaque URL for subsequent PUT queries to upload byte ranges of the file. It contains the appropriate auth token for subsequent PUT queries that expire by expirationDateTime. Do not customize this URL.

The nextExpectedRanges property specifies the next file byte location to upload from, for example, "NextExpectedRanges":["2097152"]. You must upload bytes in a file in order.

Examples

Example 1: Create an upload session to add a large attachment to a draft message

The following example shows how to create an upload session that you can use in subsequent file upload operations to the specified message.

Request

POST https://graph.microsoft.com/beta/me/messages/AAMkADI5MAAIT3drCAAA=/attachments/createUploadSession
Content-type: application/json

{
  "AttachmentItem": {
    "attachmentType": "file",
    "name": "flower",
    "size": 3483322
  }
}

Response

Note: The response object shown here might be shortened for readability.

HTTP/1.1 201 Created
Content-type: application/json

{
    "@odata.context": "https://graph.microsoft.com/beta/$metadata#microsoft.graph.uploadSession",
    "uploadUrl": "https://outlook.office.com/api/beta/Users('a8e8e219-4931-95c1-b73d-62626fd79c32@72aa88bf-76f0-494f-91ab-2d7cd730db47')/Messages('AAMkADI5MAAIT3drCAAA=')/AttachmentSessions('AAMkADI5MAAIT3k0uAAA=')?authtoken=eyJhbGciOiJSUzI1NiIsImtpZCI6IktmYUNIUlN6bllHMmNI",
    "expirationDateTime": "2019-09-25T01:09:30.7671707Z",
    "nextExpectedRanges": [
        "0-"
    ]
}

Example 2: Create an upload session to add a large in-line attachment to a draft message

The following example shows how to create an upload session that can be used to add a large inline attachment to a draft message.

For an inline attachment, set isInline property to true and use the contentId property to specify a CID for the attachment as shown below. In the body of the draft message, use the same CID value to indicate the position where you want to include the attachment using a CID HTML reference tag, for example <img src="cid:my_inline_picture">. Upon successfully uploading the file, the rendered message will include the attachment as part of the message body in the specified location.

Request

POST https://graph.microsoft.com/beta/me/messages/AAMkAGUwNjQ4ZjIxLTQ3Y2YtNDViMi1iZjc4LTMA=/attachments/createUploadSession
Content-type: application/json

{
  "AttachmentItem": {
    "attachmentType": "file",
    "name": "scenary",
    "size": 7208534,
    "isInline": true,
    "contentId": "my_inline_picture"
  }
}

Response

Note: The response object shown here might be shortened for readability.

HTTP/1.1 201 Created
Content-type: application/json

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#microsoft.graph.uploadSession",
    "uploadUrl": "https://outlook.office.com/api/gv1.0/users('a8e8e219-4931-95c1-b73d-62626fd79c32@72aa88bf-76f0-494f-91ab-2d7cd730db47')/messages('AAMkAGUwNjQ4ZjIxLTQ3Y2YtNDViMi1iZjc4LTMA=')/AttachmentSessions('AAMkAGUwNjQ4ZjIxLTAAA=')?authtoken=eyJhbGciOiJSUzI1NiIsImtpZCI6IjFTeXQ1bXdXYVh5UFJ",
    "expirationDateTime": "2021-12-27T14:20:12.9708933Z",
    "nextExpectedRanges": [
        "0-"
    ]
}