| author | spgraph-docs-team |
|---|---|
| title | Download driveItem content |
| ms.localizationpriority | high |
| ms.subservice | sharepoint |
| description | Download the contents of the primary stream (file) of a driveItem. Only driveItems with the file property can be downloaded. |
| doc_type | apiPageType |
| ms.date | 06/12/2024 |
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.
[!INCLUDE national-cloud-support]
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.
[!INCLUDE permissions-table]
[!INCLUDE app-permissions]
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
[!INCLUDE sample-code] [!INCLUDE sdk-documentation]
[!INCLUDE sample-code] [!INCLUDE sdk-documentation]
[!INCLUDE sample-code] [!INCLUDE sdk-documentation]
[!INCLUDE sample-code] [!INCLUDE sdk-documentation]
[!INCLUDE sample-code] [!INCLUDE sdk-documentation]
[!INCLUDE sample-code] [!INCLUDE sdk-documentation]
[!INCLUDE sample-code] [!INCLUDE sdk-documentation]
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 valid for a limited time. Use them immediately, as they might expire within minutes. You don't need to include an Authorization header when you access the download URL.
HTTP/1.1 302 Found
Location: https://b0mpua-by3301.files.1drv.com/y23vmagahszhxzlcvhasdhasghasodfiTo 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.downloadUrlThe 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-1023The 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.