> ## Documentation Index
> Fetch the complete documentation index at: https://developer.box.com/llms.txt
> Use this file to discover all available pages before exploring further.

# ファイルのダウンロード

export const MultiRelatedLinks = ({sections = []}) => {
  if (!sections || sections.length === 0) {
    return null;
  }
  return <div className="space-y-8">
      {sections.map((section, index) => <RelatedLinks key={index} title={section.title} items={section.items} />)}
    </div>;
};

export const RelatedLinks = ({title, items = []}) => {
  const getBadgeClass = badge => {
    if (!badge) return "badge-default";
    const badgeType = badge.toLowerCase().replace(/\s+/g, "-");
    return `badge-${badge === "ガイド" ? "guide" : badgeType}`;
  };
  if (!items || items.length === 0) {
    return null;
  }
  return <div className="my-8">
      {}
      <h3 className="text-sm font-bold uppercase tracking-wider mb-4">{title}</h3>

      {}
      <div className="flex flex-col gap-3">
        {items.map((item, index) => <a key={index} href={item.href} className="py-2 px-3 rounded related_link hover:bg-[#f2f2f2] dark:hover:bg-[#111827] flex items-center gap-3 group no-underline hover:no-underline border-b-0">
            {}
            <span className={`px-2 py-1 rounded-full text-xs font-semibold uppercase tracking-wide flex-shrink-0 ${getBadgeClass(item.badge)}`}>
              {item.badge}
            </span>

            {}
            <span className="text-base">{item.label}</span>
          </a>)}
      </div>
    </div>;
};

export const Link = ({href, children, className, ...props}) => {
  const localizedHref = localizeLink(href);
  return <a href={localizedHref} className={className} {...props}>
      {children}
    </a>;
};

ファイルをダウンロードするには、取得するコンテンツが含まれるファイルのIDを<Link href="/reference/get-files-id-content">`GET /files/:id/content`</Link>に渡します。

<CodeGroup>
  ```sh cURL theme={null}
  curl -i -L -X GET "https://api.box.com/2.0/files/12345/content" \
       -H "authorization: Bearer <ACCESS_TOKEN>" 
  ```

  ```typescript Node/TypeScript v10 theme={null}
  const fs = require('fs');

  const fileContent = await client.downloads.downloadFile('123456789');
  const fileWriteStream = fs.createWriteStream('file.pdf');
  fileContent.pipe(fileWriteStream);
  ```

  ```python Python v10 theme={null}
  client.downloads.download_file(uploaded_file.id)
  ```

  ```cs .NET v10 theme={null}
  await client.Downloads.DownloadFileAsync(fileId: uploadedFile.Id);
  ```

  ```swift Swift v10 theme={null}
  let downloadsDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
  let destinationURL = downloadsDirectoryURL.appendingPathComponent("file.txt")

  let url = try await client.downloads.downloadFile(fileId: file.id, downloadDestinationURL: destinationURL)

  if let fileContent = try? String(contentsOf: url, encoding: .utf8) {
      print("The content of the file: \(fileContent)")
  }
  ```

  ```java Java v10 theme={null}
  client.getDownloads().downloadFile(uploadedFile.getId())
  ```

  ```java Java v5 theme={null}
  BoxFile file = new BoxFile(api, "id");
  BoxFile.Info info = file.getInfo();

  FileOutputStream stream = new FileOutputStream(info.getName());
  file.download(stream);
  stream.close();
  ```

  ```python Python v4 theme={null}
  file_id = '11111'
  file_content = client.file(file_id).content()
  ```

  ```cs .NET v6 theme={null}
  Stream fileContents = await client.FilesManager.DownloadStreamAsync(id: "11111");
  ```

  ```javascript Node v4 theme={null}
  var fs = require('fs');
  client.files.getReadStream('12345', null, function(error, stream) {

  	if (error) {
  		// handle error
  	}

  	// write the file to disk
  	var output = fs.createWriteStream('/path/to/file');
  	stream.pipe(output);
  });
  ```
</CodeGroup>

## ダウンロードURL

SDKを使用しない場合、このAPIコールでは、`HTTP 302 Found`ステータスコードとともに、次のようなダウンロードURLへのリンクを含む`location`ヘッダーが返されます。

```sh theme={null}
https://dl.boxcloud.com/d/1/[long-random-string]/download
```

cURLで`-L`フラグを使用することで、自動的にこのリダイレクトに従うことができます。

<Info>
  SDKでは、結果として、バイナリデータがダウンロードされます。APIでは、ダウンロードURLが`location`ヘッダーを介して返されます。

  また、SDKを介して<Link href="/guides/downloads/get-url">ダウンロードURLを取得</Link>することも可能です。
</Info>

## ダウンロードURLの有効期限

このダウンロードURLは、ファイルのダウンロードを許可するためにユーザーのブラウザに渡すことができますが、このURLが期限切れになると、その後でダウンロードするには再度リクエストする必要があります。

## ファイルの準備ができていない

ファイルをダウンロードする準備がまだできていない場合は、クライアントがファイルをダウンロードできるようになるまでの秒数を示す`retry-after`ヘッダーが返されます。

このレスポンスは、ダウンロードリクエストの直前にファイルがアップロードされた場合に発生することがあります。

<RelatedLinks
  title="関連するAPI"
  items={[
{ label: translate("Download file"), href: "/reference/get-files-id-content", badge: "GET" }
]}
/>

<RelatedLinks
  title="関連するガイド"
  items={[
{ label: translate("Upload New File"), href: "/guides/uploads/direct/file", badge: "GUIDE" }
]}
/>
