Readouble

Livewire v3 ファイルダウンロード

ファイルダウンロード

Livewireでのファイルダウンロードは、Laravel自体とほぼ同じように機能します。通常、Livewireコンポーネント内で任意のLaravelのダウンロードユーティリティを使用でき、期待どおりに動作するはずです。File downloads in Livewire work much the same as in Laravel itself. Typically, you can use any Laravel download utility inside a Livewire component, and it should work as expected.

ただし、内部的には、ファイルダウンロードは標準的なLaravelアプリケーションとは異なる方法で処理されます。Livewireを使用する場合、ファイルの内容はBase64エンコードされ、フロントエンドに送信され、クライアントから直接ダウンロードするためにバイナリにデコードされます。However, behind the scenes, file downloads are handled differently than in a standard Laravel application. When using Livewire, the file's contents are Base64 encoded, sent to the frontend, and decoded back into binary to be downloaded directly from the client.

基本的な使い方Basic usage

Livewireでファイルダウンロードをトリガーするのは、標準的なLaravelのダウンロードレスポンスを返すのと同じくらい簡単です。Triggering a file download in Livewire is as simple as returning a standard Laravel download response.

以下は、インボイスのPDFをダウンロードするための「ダウンロード」ボタンを含むShowInvoiceコンポーネントの例です。Below is an example of a ShowInvoice component that contains a "Download" button to download the invoice PDF:

<?php

namespace App\Livewire;

use Livewire\Component;
use App\Models\Invoice;

class ShowInvoice extends Component
{
    public Invoice $invoice;

    public function mount(Invoice $invoice)
    {
        $this->invoice = $invoice;
    }

    public function download()
    {
        return response()->download( // [tl! highlight:2]
            $this->invoice->file_path, 'invoice.pdf'
        );
    }

    public function render()
    {
        return view('livewire.show-invoice');
    }
}
<div>
    <h1>{{ $invoice->title }}</h1>

    <span>{{ $invoice->date }}</span>
    <span>{{ $invoice->amount }}</span>

    <button type="button" wire:click="download">Download</button> <!-- [tl! highlight] -->
</div>

Laravelコントローラと同様に、Storageファサードを使用してダウンロードを開始することもできます。Just like in a Laravel controller, you can also use the Storage facade to initiate downloads:

public function download()
{
    return Storage::disk('invoices')->download('invoice.csv');
}

ストリーミングダウンロードStreaming downloads

Livewireはストリーミングダウンロードもできます。ただし、これらは実際にはストリーミングされません。ダウンロードは、ファイルの内容が収集され、ブラウザに配信されるまでトリガーされません。Livewire can also stream downloads; however, they aren't truly streamed. The download isn't triggered until the file's contents are collected and delivered to the browser:

public function download()
{
    return response()->streamDownload(function () {
        echo '...'; // ダウンロードコンテンツを直接エコー...
    }, 'invoice.pdf');
}

ファイルダウンロードのテストTesting file downloads

Livewireは、指定された名前でファイルがダウンロードされたことを簡単にテストするための->assertFileDownloaded()メソッドも提供します。Livewire also provides a ->assertFileDownloaded() method to easily test that a file was downloaded with a given name:

use App\Models\Invoice;

public function test_can_download_invoice()
{
    $invoice = Invoice::factory();

    Livewire::test(ShowInvoice::class)
        ->call('download')
        ->assertFileDownloaded('invoice.pdf');
}

->assertNoFileDownloaded()メソッドを使用して、ファイルがダウンロードされなかったことを確認することもできます。You can also test to ensure a file was not downloaded using the ->assertNoFileDownloaded() method:

use App\Models\Invoice;

public function test_does_not_download_invoice_if_unauthorised()
{
    $invoice = Invoice::factory();

    Livewire::test(ShowInvoice::class)
        ->call('download')
        ->assertNoFileDownloaded();
}

章選択

パッケージ

設定

バージョン変更
linkv3 linkv2
明暗テーマ
light_mode
dark_mode
brightness_auto システム設定に合わせる
テーマ選択
photo_size_select_actual デフォルト
photo_size_select_actual モノクローム(白黒)
photo_size_select_actual Solarized風
photo_size_select_actual GitHub風(青ベース)
photo_size_select_actual Viva(黄緑ベース)
photo_size_select_actual Happy(紫ベース)
photo_size_select_actual Mint(緑ベース)
コードハイライトテーマ選択

明暗テーマごとに、コードハイライトのテーマを指定できます。

テーマ配色確認
スクリーン表示幅
640px
80%
90%
100%

768px以上の幅があるときのドキュメント部分表示幅です。

インデント
無し
1rem
2rem
3rem
原文確認
原文を全行表示
原文を一行ずつ表示
使用しない

※ 段落末のEボタンへカーソルオンで原文をPopupします。

Diff表示形式
色分けのみで区別
行頭の±で区別
削除線と追記で区別

※ [tl!…]形式の挿入削除行の表示形式です。

テストコード表示
両コード表示
Pestのみ表示
PHPUnitのみ表示
OS表示
全OS表示
macOSのみ表示
windowsのみ表示
linuxのみ表示
JSフレームワーク
両フレームワーク
Reactのみ表示
Vueのみ表示
JSのみ表示

(JSが存在しない場合は、他を全表示)

和文変換

対象文字列と置換文字列を半角スペースで区切ってください。(最大5組各10文字まで)

本文フォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

コードフォント

総称名以外はCSSと同様に、"〜"でエスケープしてください。

保存内容リセット

localStrageに保存してある設定項目をすべて削除し、デフォルト状態へ戻します。

ヘッダー項目移動

キーボード操作