<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 流處理文件 > 這個章節將向你展示如何在`HTTP應用`中流處理文件。以下例子不適用于GraphQL或者微服務應用。 有時你可能想從你的REST API向客戶端發送文件,想要在Nest中做到這一點,你可能會像下面這樣做: ```typescript @Controller('file') export class FileController { @Get() getFile(@Res() res: Response) { const file = createReadStream(join(process.cwd(), 'package.json')); file.pipe(res); } } ``` 但是這樣一來,你就會丟失控制器之后的攔截器邏輯。想要避免這一點,你可以返回一個`StreamableFile`實例,框架會幫你使用管道傳輸響應。 ### 流式文件類(Streamable File) `StreamableFile`是一個持有要返回的流的類。你可以傳入一個`Buffer`或者`Stream`到`StreamableFile`類的構造函數來創建一個新的`StreamableFile`實例。 > `StreamableFile`類可以從`@nestjs/common`中導入 ### 跨平臺支持 默認情況下,Fastify服務器可以不通過`stream.pipe(res)`直接發送文件,所以你并不需要使用`StreamableFile`類。但是,Nest仍然支持在所有這些類型的平臺上使用`StreamableFile`,所以即使你需要在Express和Fastify之間切換,也不需要擔心這兩個引擎上的兼容性問題。 ### 例子 下面是一個作為文件而不是JSON返回`package.json`的例子,當然,它也適用于圖片、文檔和所有其他類型的文件。 ```typescript import { Controller, Get, StreamableFile } from '@nestjs/common'; import { createReadStream } from 'fs'; import { join } from 'path'; @Controller('file') export class FileController { @Get() getFile(): StreamableFile { const file = createReadStream(join(process.cwd(), 'package.json')); return new StreamableFile(file); } } ``` 返回的默認類型是 `application/octet-stream`,如果你需要自定義響應類型,你可以使用 `res.set` 方法。 ```typescript import { Controller, Get, StreamableFile, Response } from '@nestjs/common'; import { createReadStream } from 'fs'; import { join } from 'path'; @Controller('file') export class FileController { @Get() getFile(@Response({ passthrough: true }) res): StreamableFile { const file = createReadStream(join(process.cwd(), 'package.json')); res.set({ 'Content-Type': 'application/json', 'Content-Disposition': 'attachment; filename="package.json"', }); return new StreamableFile(file); } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看