Download driveItem content
Namespace: microsoft.graph
Download the contents of the primary stream (file) of a driveItem. Only driveItem objects with the file property can be downloaded.
To get the ID of the item to be downloaded, see Get driveItem.
This API is available in the following national cloud deployments.
Global service | US Government L4 | US Government L5 (DOD) | China operated by 21Vianet |
---|---|---|---|
✅ | ✅ | ✅ | ✅ |
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) | 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 |
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
Name | Description |
---|---|
Authorization | Bearer {token}. Required. Learn more about authentication and authorization. |
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. |
Here's an example to download a complete file.
GET /me/drive/items/{item-id}/content
Returns a 302 Found
response redirecting to a preauthenticated download URL for the file, which is the same URL available through the @microsoft.graph.downloadUrl
property on the DriveItem.
To download the contents of the file your application needs to follow the Location
header in the response.
Many HTTP client libraries will automatically follow the 302 redirection and start downloading the file immediately.
Preauthenticated download URLs are only valid for a short period of time (a few minutes) and don't require an Authorization
header to download.
HTTP/1.1 302 Found
Location: https://b0mpua-by3301.files.1drv.com/y23vmagahszhxzlcvhasdhasghasodfi
To download files in a JavaScript app, you can't 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 preauthenticated, they can be retrieved without a CORS preflight request.
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
The call 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.
To download a partial range of bytes from the file, your app can use the Range
header as specified in RFC 2616.
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
The call returns an HTTP 206 Partial Content
response with the request range of bytes from the file.
If the range can't 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>
See Error Responses for more info about how errors are returned.