Download the contents of a DriveItem
Namespace: microsoft.graph
Warning
This endpoint requires TLS 1.2 to function normally. Microsoft announced the deprecation of TLS 1.0 and 1.1 for Office 365 and Azure AD services. Although Microsoft Graph still supports these two protocols, you might experience transport-level errors. For more information about the TLS 1.0 and 1.1 deprecation, see Enable support for TLS 1.2 in your environment.
Download the contents of the primary stream (file) of a driveItem. Only driveItems with the file property can be downloaded.
Permissions
One of the following permissions is required to call this API. To learn more, including how to choose permissions, see Permissions.
Permission type | Permissions (from least to most privileged) |
---|---|
Delegated (work or school account) | Files.Read, Files.ReadWrite, Files.Read.All, Files.ReadWrite.All, Sites.Read.All, Sites.ReadWrite.All |
Delegated (personal Microsoft account) | Files.Read, Files.ReadWrite, Files.Read.All, Files.ReadWrite.All |
Application | Files.Read.All, Files.ReadWrite.All, Sites.Read.All, Sites.ReadWrite.All |
HTTP request
GET /drives/{drive-id}/items/{item-id}/content
GET /groups/{group-id}/drive/items/{item-id}/content
GET /me/drive/root:/{item-path}:/content
GET /me/drive/items/{item-id}/content
GET /shares/{shareIdOrEncodedSharingUrl}/driveItem/content
GET /sites/{siteId}/drive/items/{item-id}/content
GET /users/{userId}/drive/items/{item-id}/content
Optional request headers
Name | Value | Description |
---|---|---|
if-none-match | String | If this request header is included and the eTag (or cTag) provided matches the current tag on the file, an HTTP 304 Not Modified response is returned. |
Example
Here is an example to download a complete file.
Request
GET /me/drive/items/{item-id}/content
Response
Returns a 302 Found
response redirecting to a pre-authenticated download URL for the file.
This is the same URL available through the @microsoft.graph.downloadUrl
property on the DriveItem.
To download the contents of the file your application will need to follow the Location
header in the response.
Many HTTP client libraries will automatically follow the 302 redirection and start downloading the file immediately.
Pre-authenticated download URLs are only valid for a short period of time (a few minutes) and do not require an Authorization
header to download.
HTTP/1.1 302 Found
Location: https://b0mpua-by3301.files.1drv.com/y23vmagahszhxzlcvhasdhasghasodfi
Downloading files in JavaScript apps
To download files in a JavaScript app, you cannot use the /content
API, because this responds with a 302
redirect.
A 302
redirect is explicitly prohibited when a Cross-Origin Resource Sharing (CORS) preflight is required, such as when providing the Authorization header.
Instead, your app needs to select the @microsoft.graph.downloadUrl
property, which returns the same URL that /content
directs to.
This URL can then be requested directly using XMLHttpRequest.
Because these URLs are pre-authenticated, they can be retrieved without a CORS preflight request.
Example
To retrieve the download URL for a file, first make a request that includes the @microsoft.graph.downloadUrl
property:
GET /drive/items/{item-ID}?select=id,@microsoft.graph.downloadUrl
This returns the ID and download URL for a file:
HTTP/1.1 200 OK
Content-Type: application/json
{
"id": "12319191!11919",
"@microsoft.graph.downloadUrl": "https://..."
}
You can then make an XMLHttpRequest for the URL provided in @microsoft.graph.downloadUrl
to retrieve the file.
Partial range downloads
To download a partial range of bytes from the file, your app can use the Range
header as specified in RFC 2616.
Note that you must append the Range
header to the actual @microsoft.graph.downloadUrl
URL and not to the request for /content
.
GET https://b0mpua-by3301.files.1drv.com/y23vmag
Range: bytes=0-1023
This will return an HTTP 206 Partial Content
response with the request range of bytes from the file.
If the range cannot be generated the Range header may be ignored and an HTTP 200
response would be returned with the full contents of the file.
HTTP/1.1 206 Partial Content
Content-Range: bytes 0-1023/2048
Content-Type: application/octet-stream
<first 1024 bytes of file>
Error responses
See Error Responses for more info about how errors are returned.
Feedback
Submit and view feedback for